function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HarmpieHarmpie 

Question on triggers and testmethods

See my trigger:
Code:
trigger copyCursusDataToOpp on Cursusinschrijving__c (after insert, after update) {

 if(System.Trigger.isUpdate) {
  for(Cursusinschrijving__c ci : Trigger.new) {
   if(ci.Via_opportunity__c!=null) {
    Opportunity Opp = [SELECT Id,Name FROM Opportunity WHERE Id = :ci.Via_opportunity__c];
    Cursus C = [SELECT Id,Cursusdagen__c FROM Cursus__c WHERE Id = :ci.Cursus__c  LIMIT 1];
    Opp.Cursusdata__c = C.Cursusdagen__c;
    Opp.Cursus__c = ci.Cursus__c;
    update Opp;   
   }
  } 
 } else if(System.Trigger.isInsert) {
  for(Cursusinschrijving__c ci : Trigger.new) {
   if(ci.Via_opportunity__c!=null) {
    Opportunity Opp = [SELECT Id,Name FROM Opportunity WHERE Id = :ci.Via_opportunity__c];
    Cursus C = [SELECT Id,Cursusdagen__c FROM Cursus__c WHERE Id = :ci.Cursus__c LIMIT 1];
    Opp.Cursusdata__c = C.Cursusdagen__c;
    Opp.Cursus__c = ci.Cursus__c;
    update Opp;   
   }
  } 
 }
 static testMethod void testTrigger() {
  Cursus__c tc = new Cursus__c();
  insert tc;
  Cursusinschrijving__c tci = new Cursusinschrijving__c();
  Opportunity to = new Opportunity();
  to.Name = 'testopp';
  insert to;
  tci.Via_opportunity__c = to.Id;
  tci.Cursus__c = tc.id;
  insert tci;
  System.assertEquals(to.Cursus__c, tc.Id); 
 }
}

 
I got a few questions on this:
1) Why does ci.Cursus__r.Cursusdagen__c return null in the first parts, while the query to the related object (as in the example) DOES return a value?
2) Saving the code above gives an error: Compile Error: Only top-level class methods can be declared static at line 2 column 748. Can someone tell me how to write testmethods on triggers?
3) When trying to deploy the trigger (without any testcoverage at that time), selecting only the trigger to be deployed in the deploymentplan using Eclipse, I will receive an error, complaining about testcoverage for multiple APEX components (while only the trigger is selected in the deployment plan !!), because the trigger is untested, and my other APEX class has minimal testcoverage (78%), the average of coverage drops below 75%, making deployment impossible. Why does eclipse take unselected component's testcoverage into account ?
Ron HessRon Hess
i don't see ci.Cursus__r.Cursusdagen__c in your code?

test methods must be in a class, as the message indicates, please create a class for this test method (not inside a trigger)

you must select a trigger and the class that tests it in the deploy plan.
HarmpieHarmpie
Thanks Ron.
 
The ci.Cursus__r.Cursusdagen__c was replaced by
 
Code:
    Cursus C = [SELECT Id,Cursusdagen__c FROM Cursus__c WHERE Id = :ci.Cursus__c LIMIT 1];
    Opp.Cursusdata__c = C.Cursusdagen__c;

 
because it returned null. Since the code I used now does not return null, but the correct value, I thought it is quite strange   that ci.Cursus__r.Cursusdagen__c did not return anything.
 
I will try putting the testmethod in a class.
HarmpieHarmpie
Now that the testmethod is in a class, it seems to go a lot better deploying, except for 1 thing. I keep getting an error from the test
 
Code:
FIELD_CUSTOM_VALIDATION_EXCEPTION, Ins:CTR-20083331: [Id]

I think I know what`s causing this, but I don`t see why my test would fail over it. In the system I am testing with, a workflow updates the Opportunity name at save or update time with an autonumber value. This seems to be happening in this case too.
 
Here is the testmethod again
Code:
public class testClass {
 static testMethod void testTrigger() {
  Cursus__c tc = new Cursus__c();
  insert tc;
  Cursusinschrijving__c tci = new Cursusinschrijving__c();
  Opportunity to = new Opportunity(Name='TestOpp',StageName='Closed/Won',CloseDate=System.today());
  insert to;
  tci.Via_opportunity__c = to.Id;
  tci.Cursus__c = tc.id;
  insert tci;
  System.assertEquals(to.Cursus__c, tc.Id); 
 }
}

 

Ron HessRon Hess
yes, field validation is run / tested by test methods also.

so your custom field validation is fireing and preventing your test from creating an opportunity, i think
HarmpieHarmpie
Issue here is, that it is no validation rule, simply a workflow rule with a field update attached, which copies a value from an autonumberfield on the opportunity (the CTR-xxxxx value you see in the message), into the opportunity name. This works just fine when creating an opportunity in SFDC. How can this obstruct my test?


Message Edited by Harmpie on 03-06-2008 08:48 AM
Ron HessRon Hess
try disabling all workflow rules and see if this test passes, otherwise i don't see anything wrong with the test.