You need to sign in to do that
Don't have an account?
Gavin Britto
Test coverage Help
A consultant wrote a Opportunity Trigger but no code coverage. Any help with developing a test class would be appreciated, so I could deploy the Trigger below to production.
trigger Opp_Trigger on Opportunity (after insert, after update) { if ((!Util.DoNotRunTrigger) && (Trigger.IsAfter) && (!Archtics.HasSentArchticsCreate)) { /* create in Archtics */ set<Id> accids = new set<Id>(); set<Id> ownerids = new set<Id>(); for (Opportunity o : Trigger.new) { Opportunity oldopp; if (Trigger.IsUpdate) oldopp = Trigger.oldmap.get(o.Id); accids.add(o.AccountId); ownerids.add(o.OwnerId); } map<Id, Account> accid2accmap = new map<Id, Account>(); if (accids.size() > 0) { accid2accmap = new map<Id, Account>([select Id, Update_in_Archtics_Wizards__c, Update_in_Archtics_Mystics__c, Update_in_Archtics_Capitals__c, Update_in_Archtics_MSE__c, Create_in_Archtics_Wizards__c, Create_in_Archtics_Mystics__c, Create_in_Archtics_Capitals__c,Create_in_Archtics_MSE__c, Acct_Rep_ID_Capitals__c, Acct_Rep_ID_Mystics__c, Acct_Rep_ID_Wizards__c, Archtics_ID_Wizards__c, Archtics_ID_Mystics__c, Archtics_ID_Caps__c,Archtics_ID_MSE__c from Account where Id in :accids]); } map<Id, User> userid2usermap = new map<Id, User>(); if (ownerids.size() > 0) { userid2usermap = new map<Id, User>([ select Id, Archtics_Rep_ID_Wiz__c, Archtics_Rep_ID_Mystics__c, Archtics_Rep_ID_Caps__c from User where Id in :ownerids]); } // set<Id> capitalsaccids = new set<Id>(); //set<Id> mysticsaccids = new set<Id>(); // set<Id> wizardsaccids = new set<Id>(); set<Id> MSEaccids = new set<Id>(); for (Opportunity o : Trigger.new) { if ((!o.Name.contains('Auto-created Opportunity')) && (o.Product_Category__c != null)) { if (o.Product_Category__c.contains('Caps')) { system.debug('\n\n42 caps found'); Account thisacc = accid2accmap.get(o.AccountID); if ( (thisacc != null) && (thisacc.Archtics_ID_MSE__c == null) && ( (o.StageName=='Completed Sale') || (o.StageName=='Hot') || (o.StageName=='Comp Requested') ) ) { thisacc.Acct_Rep_ID_Capitals__c = userid2usermap.get(o.OwnerId).Archtics_Rep_ID_Caps__c; accid2accmap.put(o.AccountId, thisacc); //capitalsaccids.add(o.AccountId); // thisacc.Acct_Rep_ID_MSE__c = userid2usermap.get(o.OwnerId).Archtics_Rep_ID_MSE__c; MSEaccids.add(o.AccountId); } } if ((o.Product_Category__c.contains('Wizards') || (o.Product_Category__c.contains('Valor') || (o.Product_Category__c.contains('Family Shows/Events and Concerts'))))) { Account thisacc = accid2accmap.get(o.AccountID); if ((thisacc != null) && (thisacc.Archtics_ID_MSE__c == null) && ( (o.StageName=='Completed Sale') || (o.StageName=='Hot') || (o.StageName=='Comp Requested') ) ) { thisacc.Acct_Rep_ID_Wizards__c = userid2usermap.get(o.OwnerId).Archtics_Rep_ID_Wiz__c; accid2accmap.put(o.AccountId, thisacc); MSEaccids.add(o.AccountId); } } if (o.Product_Category__c.contains('Mystics')) { Account thisacc = accid2accmap.get(o.AccountID); if ((thisacc != null) && (thisacc.Archtics_ID_MSE__c == null) && ( (o.StageName=='Completed Sale') || (o.StageName=='Hot') ||(o.StageName=='Comp Requested') ) ) { thisacc.Acct_Rep_ID_Mystics__c = userid2usermap.get(o.OwnerId).Archtics_Rep_ID_Mystics__c; accid2accmap.put(o.AccountId, thisacc); MSEaccids.add(o.AccountId); // mystics now created in MSE as well } } } } if (accid2accmap.size() > 0) update accid2accmap.values(); //if (capitalsaccids.size() > 0) //Archtics.createFuture(capitalsaccids, 'Capitals'); //if (wizardsaccids.size() > 0) //Archtics.createFuture(wizardsaccids, 'Wizards'); //changed from wizardsaccids to MSEaccids 130711 JN if (MSEaccids.size() > 0) Archtics.createAccountsInArchtics(MSEaccids, 'MSE'); Archtics.HasSentArchticsCreate = TRUE; } //end if IsAfter }
Without knowing your validation rules and other things - you never know what all is needed. But in order to test a trigger - what do you have to do? You have to do a DML operation (ie insert/update) on the object of the trigger. In this case - your trigger is on the Opportunity object - so the basics of writing test code is writing a class and method that creates or updates the data specified in the trigger.
So, the code above should get at-least the first few lines covered. But you never know - like I said - if you've got validation rules - the insert to Accounts and then the insert to Opportunites might actually fail, as not all of the fields are specified.
All Answers
Without knowing your validation rules and other things - you never know what all is needed. But in order to test a trigger - what do you have to do? You have to do a DML operation (ie insert/update) on the object of the trigger. In this case - your trigger is on the Opportunity object - so the basics of writing test code is writing a class and method that creates or updates the data specified in the trigger.
So, the code above should get at-least the first few lines covered. But you never know - like I said - if you've got validation rules - the insert to Accounts and then the insert to Opportunites might actually fail, as not all of the fields are specified.
1) http://amitsalesforce.blogspot.com/search/label/Test%20Class
2) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
Please try below test class.
Please follow below salesforce Best Practice for Test Classes :-
1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .
Please let us know if this post will help you
Thank you for the detailed explanation and writing this test class. Do any of the code you've written go inside my Opp tirgger or do they stay in a seperate class of its own? I ask because I tried installing the Opp Trigger & the test class in production and it still has 0% code coverage. So the installation wasn't succesful
From Eclipse:
Right click on the test class, and select Run-As
Specify your Project (pointing to the sandbox)
Specify the test class under the Test tab
click Apply, and Run.
This should run the test class, and you'll see the test results at the bottom in the Apex Test Results tab. This will tell you if you're getting any coverage, before you try to deploy it. If you still have no coverage - check the logs, and make sure you're actually inserting the Account and the Opportunity records - I would assume that's not happening.