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
Will_1Will_1 

Another Test Coverage Problem - Trigger and Class

The newbie is back again...  I now have all of my code working both in Eclipse and in my development environment.  Here is my trigger which fires on the Insert or Update of a new custom object, Job:
Code:
trigger TargetMkups on Job__c (before insert, before update) {
 JobCalculations.updateTargetMkups(trigger.new);
}

 
Below is the class and method called in the trigger:
Code:
public class JobCalculations {
 
public static void updateTargetMkups(Job__c[] myjob) {
 
 Set<Id> jobId = new Set<Id>();
 for (Job__c j:myjob) { 
  if (j.opportunity__c != null){
   jobId.add(j.opportunity__c);
  }
    
  Opportunity o = [ SELECT  Id, AccountId
       FROM Opportunity
       WHERE Id in : jobId];
      
  Account a =  [ SELECT  Id, target_c2c_mkup__c, target_w2h_mkup__c, target_w2b_mkup__c
       FROM Account
       WHERE Id = :o.AccountId];
        
  j.target_c2c_mkup__c = a.target_c2c_mkup__c;
  j.target_w2h_mkup__c = a.target_w2h_mkup__c;
  j.target_w2b_mkup__c = a.target_w2b_mkup__c;
 }
}
}

 
And, here is my test method which inserts one account, one opportunity, and one job which, given the simplicity of this code, I would think should achieve ample coverage:
Code:
public class JobCalculationstest {

public static testMethod void testUpdateTargetMkups() {
 Account a1 = new Account (name = 'Test Acct', target_c2c_mkup__c = 1.4, target_w2b_mkup__c = 1.45, target_w2h_mkup__c = 1.45);
 insert a1;
 Opportunity o1 = new Opportunity (name = 'Test Opp', accountid = a1.id, stageName ='Open', CloseDate=Date.newInstance(2008,5,15));
 insert o1;
 Job__c j1 = new Job__c (name = 'Test Job', opportunity__c = o1.id);
 insert j1;
 Job__c queryJob1 = [select Id, target_c2c_mkup__c, target_w2b_mkup__c, target_w2h_mkup__c from Job__c where Id = :j1.Id];
 System.assertEquals(1.4, queryJob1.target_c2c_mkup__c);
 System.assertEquals(1.45, queryJob1.target_w2b_mkup__c);
 System.assertEquals(1.45, queryJob1.target_w2h_mkup__c);  
}
}

 However, I still get 0% coverage from the test above.  As I said, I know the code works because I've tested it in my development environment.  Additionally, the log file shows the updated values in the log.  I'm just stuck with this 0% coverage.  I'm so close... Any advice?
 
jrotensteinjrotenstein
Perhaps you should put your TestMethod as a procedure within your JobCalculations class instead of in its own class? That way, SFDC will know which class you are testing.

Also, although probably unrelated, I think there is a logic error in your updateTargetMkups procedure:
  • For each incoming Job_c, it potentially does jobId.add(j.opportunity__c)
  • Therefore, the SELECT ... FROM Opportunity might return multiple Opportunities (after the first iteration)
  • Subsequent iterations might be using the first returned Opportunity, rather than the one matching the Job
  • I would suggest having your test create multiple jobs, pointing to different accounts
  • In fact, I'm not sure why you are using the JobId array
If you are just trying to get Job_c.Opportunity.Account.target_c2c_mkup__c, you could try a parent/child select, like:
SELECT
Account.target_c2c_mkup__c,
Account.target_w2h_mkup__c,
Account.target_w2b_mkup__c
FROM Opportunity
WHERE Id = myJob.Opportunity__c


Message Edited by jrotenstein on 05-19-2008 06:06 PM
Will_1Will_1

John,

Thanks for your help.  I removed the jobId array in order to remove the possibility for the error you mention.  I also put the test method in the same class directly before my actual method.  Here is the revised class with the test method and jobId changes:

Code:
public class JobCalculations {

public static testMethod void testUpdateTargetMkups() {
 Account a1 = new Account (name = 'Test Acct', target_c2c_mkup__c = 1.4, target_w2b_mkup__c = 1.45, target_w2h_mkup__c = 1.45);
 insert a1;
 Opportunity o1 = new Opportunity (name = 'Test Opp', accountid = a1.id, stageName ='Open', CloseDate=Date.newInstance(2008,5,15));
 insert o1;
 Job__c j1 = new Job__c (name = 'Test Job', opportunity__c = o1.id);
 insert j1;
 Job__c queryJob1 = [select Id, target_c2c_mkup__c, target_w2b_mkup__c, target_w2h_mkup__c from Job__c where Id = :j1.Id];
 System.assertEquals(1.4, queryJob1.target_c2c_mkup__c);
 System.assertEquals(1.45, queryJob1.target_w2b_mkup__c);
 System.assertEquals(1.45, queryJob1.target_w2h_mkup__c);  
}
 
public static void updateTargetMkups(Job__c[] myjob) {
 
 for (Job__c j:myjob) { 
    
  Opportunity o = [ SELECT  Id, AccountId
       FROM Opportunity
       WHERE Id = : j.Opportunity__c];
      
  Account a =  [ SELECT  Id, target_c2c_mkup__c, target_w2h_mkup__c, target_w2b_mkup__c
       FROM Account
       WHERE Id = :o.AccountId];
        
  j.target_c2c_mkup__c = a.target_c2c_mkup__c;
  j.target_w2h_mkup__c = a.target_w2h_mkup__c;
  j.target_w2b_mkup__c = a.target_w2b_mkup__c;
 }
}
}

Below, you can see a copy of the error log I receive when I try to deploy this to production:

Code:
*** Beginning Test 1: JobCalculations.public static testMethod void testUpdateTargetMkups()

20080519185308.712:Class.JobCalculations: line 1, column 8: Static initialization: JobCalculations
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 4, column 5: DeclareVar: SOBJECT:Account a1
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 4, column 5:     initial value: Account:{Target_W2H_Mkup__c=1.45, Target_W2B_Mkup__c=1.45, Target_C2C_Mkup__c=1.4, Name=Test Acct}
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 5, column 5: Insert: SOBJECT:Account
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 5, column 5:     DML Operation executed in 380 ms
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 6, column 5: DeclareVar: SOBJECT:Opportunity o1
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 6, column 5:     initial value: Opportunity:{AccountId=0014000000HgNq6AAF, StageName=Open, Name=Test Opp, CloseDate=Thu May 15 00:00:00 GMT 2008}
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 7, column 5: Insert: SOBJECT:Opportunity
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 7, column 5:     DML Operation executed in 600 ms
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 8, column 5: DeclareVar: SOBJECT:Job__c j1
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 8, column 5:     initial value: Job__c:{Opportunity__c=00640000009qzJIAAY, Name=Test Job}
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 9, column 5: Insert: SOBJECT:Job__c
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 9, column 5:     DML Operation executed in 214 ms
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 10, column 5: DeclareVar: SOBJECT:Job__c queryJob1
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 10, column 24: SOQL query with 1 row finished in 26 ms
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 10, column 5:     initial value: Job__c:{Target_W2H_Mkup__c=1.45, Target_W2B_Mkup__c=1.45, Target_C2C_Mkup__c=1.40, Id=a0640000003H2JyAAK}
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 11, column 5: System.assertEquals(Double, Double)
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 12, column 5: System.assertEquals(Double, Double)
20080519185308.712:Class.JobCalculations.testUpdateTargetMkups: line 13, column 5: System.assertEquals(Double, Double)
20080519185308.712:Class.JobCalculations: line 3, column 31:     returning from end of method public static testMethod void testUpdateTargetMkups() in 1227 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 1 out of 100
Number of query rows: 1 out of 500
Number of SOSL queries: 0 out of 20
Number of DML statements: 3 out of 100
Number of DML rows: 3 out of 500
Number of script statements: 10 out of 200000
Maximum heap size: 0 out of 500000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0
*** Ending Test JobCalculations.public static testMethod void testUpdateTargetMkups()

Cumulative profiling information:

It seems to me by reading the error log, that the code in the testmethod is executing correctly, and that columns in my Job__c record have been updated correctly.  However, for some reason SFDC does not seem to think that the trigger has been executed (which it clearly has been).  Are there any test statements I need to put directly in the trigger?  For completeness, here is my trigger:

Code:
trigger TargetMkups on Job__c (before insert, before update) {
 JobCalculations.updateTargetMkups(trigger.new);
}

Any help from John or anyone else is greatly appreciated.

Thanks,

Will
 

 

TehNrdTehNrd
Give this a try for your test class.

Code:
public static testMethod void testUpdateTargetMkups() {
 Account a1 = new Account (name = 'Test Acct', target_c2c_mkup__c = 1.4, target_w2b_mkup__c = 1.45, target_w2h_mkup__c = 1.45);
 Opportunity o1 = new Opportunity (name = 'Test Opp', accountid = a1.id, stageName ='Open', CloseDate=Date.newInstance(2008,5,15)); 
 Job__c j1 = new Job__c (name = 'Test Job', opportunity__c = o1.id);

 Test.startTest();
 insert a1;
 insert o1;
 insert j1;
 Test.stopTest();

 Job__c queryJob1 = [select Id, target_c2c_mkup__c, target_w2b_mkup__c, target_w2h_mkup__c from Job__c where Id = :j1.Id];
 System.assertEquals(1.4, queryJob1.target_c2c_mkup__c);
 System.assertEquals(1.45, queryJob1.target_w2b_mkup__c);
 System.assertEquals(1.45, queryJob1.target_w2h_mkup__c);  
}

 

Will_1Will_1
Same problem.  It doesn't make any sense to me.  When we insert the Job__c in the test method, it's definitely executing the trigger, but sf.com does not seem to think so.  Is it possible that this is a bug?
jrotensteinjrotenstein
I'd really like to see the test coverage message you are receiving.


Message Edited by jrotenstein on 05-21-2008 01:27 PM
Will_1Will_1

No problem.  I receive this error only when I try to deploy to server.  I do not get this coverage warning when I save the objects.  I also do not get this error when I "Run Tests" within the code itself in Eclipse.  I'm using Eclipse (w/ Force.com) to deploy per what I believe are the most up-to-date procedures.

So, on to the error...  In the Test Deployment Results View, I get a red General Warning that says the following:

"Test coverage of selected Apex Class and Trigger is 0%, at least 75% test coverage is required"

Below this message, there is a yellow Code Coverage Results Message that says the following:

JobCalculations (Class) -- 7 lines not tested, 0% covered

Line 21, Column 20 not covered, Line 23, Column 5 not covered, Line 25, Column 9 not covered, Line 29, Column 9 not covered, Line 33, Column 9 not covered, Line 34, Column 9 not covered, Line 35, Column 9 not covered

Here is a copy of the deployment log:

Code:
*** Deployment Log ***
Result: FAILED
Date: May 20, 2008 7:48:22 AM EDT

# Deployed From:
   Project name: Development OmniTrack
   Username: will.hardy@gmail.com
   Endpoint: https://www.salesforce.com/services/Soap/u/12.0

# Deployed To:
   Username: will.hardy@omnipointinc.com
   Endpoint: https://www.salesforce.com/services/Soap/u/12.0

# Deploy Results:
   Name:    unpackaged/classes/JobCalculations.cls
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

   Name:    unpackaged/triggers/TargetMkups.trigger
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

   Name:    unpackaged/package.xml
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

# Test Results:
  Test coverage of selected Apex Class and Trigger is 0%, at least 75% test coverage is required

Here is a copy of the Debug Log:

Code:
*** Beginning Test 1: JobCalculations.public static testMethod void testUpdateTargetMkups()

20080520114828.019:Class.JobCalculations: line 1, column 8: Static initialization: JobCalculations
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 5, column 5: DeclareVar: SOBJECT:Account a1
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 5, column 5:     initial value: Account:{Target_W2H_Mkup__c=1.45, Target_W2B_Mkup__c=1.45, Target_C2C_Mkup__c=1.4, Name=Test Acct}
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 6, column 5: DeclareVar: SOBJECT:Opportunity o1
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 6, column 5:     initial value: Opportunity:{AccountId=null, StageName=Open, Name=Test Opp, CloseDate=Thu May 15 00:00:00 GMT 2008}
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 7, column 5: DeclareVar: SOBJECT:Job__c j1
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 7, column 5:     initial value: Job__c:{Opportunity__c=null, Name=Test Job}
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 9, column 5: Test.startTest()
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 10, column 5: Insert: SOBJECT:Account
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 10, column 5:     Changing testing limit context based on DML operation of size: 1
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 10, column 5:     DML Operation executed in 296 ms
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 11, column 5: Insert: SOBJECT:Opportunity
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 11, column 5:     DML Operation executed in 564 ms
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 12, column 5: Insert: SOBJECT:Job__c
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 12, column 5:     DML Operation executed in 128 ms
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 13, column 5: Test.stopTest()
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 15, column 5: DeclareVar: SOBJECT:Job__c queryJob1
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 15, column 24: SOQL query with 1 row finished in 31 ms
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 15, column 5:     initial value: Job__c:{Target_W2H_Mkup__c=1.45, Target_W2B_Mkup__c=1.45, Target_C2C_Mkup__c=1.40, Id=a0640000003H4GiAAK}
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 16, column 5: System.assertEquals(Double, Double)
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 17, column 5: System.assertEquals(Double, Double)
20080520114828.019:Class.JobCalculations.testUpdateTargetMkups: line 18, column 5: System.assertEquals(Double, Double)
20080520114828.019:Class.JobCalculations: line 3, column 31:     returning from end of method public static testMethod void testUpdateTargetMkups() in 1041 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 1 out of 100
Number of query rows: 1 out of 500
Number of SOSL queries: 0 out of 20
Number of DML statements: 1 out of 100
Number of DML rows: 1 out of 500
Number of script statements: 9 out of 200000
Maximum heap size: 0 out of 500000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Testing resources:

Resource usage for namespace: (default)
Number of SOQL queries: 0 out of 20
Number of query rows: 0 out of 1000
Number of SOSL queries: 0 out of 0
Number of DML statements: 2 out of 20
Number of DML rows: 2 out of 100
Number of script statements: 3 out of 10200
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0
*** Ending Test JobCalculations.public static testMethod void testUpdateTargetMkups()

Cumulative profiling information:


 

 

Will678Will678

Okay...  I now know the problem is not with my Force.com installation, or with my deployment steps.  I took the advice from our friend in Australia, and I commented out the non-testing method in my class, and I took out the call to that class in my trigger and I was able to successfully deploy both the trigger and the class to my production environment.  However, when I uncomment the call to the real method, I once again get the 0% test coverage problem.  The code (with the extra test call in the trigger) is below. 

Is there an administrator out there that has a suggestion?  I really do not know what else to try.

Code:
trigger TargetMkups on Job__c (before insert, before update) {
 JobCalculations.testUpdateTargetMkups();
 JobCalculations.updateTargetMkups(trigger.new); 
}

public class JobCalculations {

public static testMethod void testUpdateTargetMkups() {

  Account a1 = new Account (name = 'Test Acct', target_c2c_mkup__c = 1.4, target_w2b_mkup__c = 1.45, target_w2h_mkup__c = 1.45);
  insert a1;
  Opportunity o1 = new Opportunity (name = 'Test Opp', accountid = a1.id, stageName ='Open', CloseDate=Date.newInstance(2008,5,15));
  insert o1; 
  Job__c j1 = new Job__c (name = 'Test Job', opportunity__c = o1.id);
  insert j1;

  Job__c queryJob1 = [select Id, target_c2c_mkup__c, target_w2b_mkup__c, target_w2h_mkup__c from Job__c where Id = :j1.Id];
  System.assertEquals(1.4, queryJob1.target_c2c_mkup__c);
  System.assertEquals(1.45, queryJob1.target_w2b_mkup__c);
  System.assertEquals(1.45, queryJob1.target_w2h_mkup__c); 
}
 
public static void updateTargetMkups(Job__c[] myjob) {
 
 for (Job__c j:myjob) { 
    
  Opportunity o = [ SELECT  Id, AccountId
       FROM Opportunity
       WHERE Id = : j.Opportunity__c];
      
  Account a =  [ SELECT  Id, target_c2c_mkup__c, target_w2h_mkup__c, target_w2b_mkup__c
       FROM Account
       WHERE Id = :o.AccountId];
        
  j.target_c2c_mkup__c = a.target_c2c_mkup__c;
  j.target_w2h_mkup__c = a.target_w2h_mkup__c;
  j.target_w2b_mkup__c = a.target_w2b_mkup__c;
 }
}
}


 

Will678Will678
I'm working through a workaround to this problem by putting all of the code directly in the trigger...  However, I think it would still be helpful for the community come to a conclusion on this particular problem.  I think there are a lot of newbies out there that may run into a similar problem testing a basic trigger and class.  Are there any of the salesforce architects out there that have any ideas on why I may be having this problem?