You need to sign in to do that
Don't have an account?
Will_1
Below is the class and method called in the trigger:
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:
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?
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?
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:Message Edited by jrotenstein on 05-19-2008 06:06 PM
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:
Below, you can see a copy of the error log I receive when I try to deploy this to production:
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:
Any help from John or anyone else is greatly appreciated.
Thanks,
Will
Message Edited by jrotenstein on 05-21-2008 01:27 PM
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:
Here is a copy of the Debug Log:
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.