You need to sign in to do that
Don't have an account?
Tania B Garcia
Apex Test is complete but coverage is 0%
Hi
Help please, I made the following trigger on Opportunity, where to being in a specific stage, create a record of a random user of a specific profile in the Opportunity Team
This is my trigger
This is my Test
Help please, I made the following trigger on Opportunity, where to being in a specific stage, create a record of a random user of a specific profile in the Opportunity Team
This is my trigger
trigger SLInsertOppTeam on Opportunity (before insert, before update) { for(Opportunity opp : trigger.New){ if(opp.StageName == 'A' && opp.is_new__c == false) { List<OpportunityTeamMember> member = [SELECT TeamMemberRole FROM OpportunityTeamMember Where OpportunityId = : opp.Id AND TeamMemberRole = : 'Analist']; if(member.size() == 0 ) { OpportunityTeamMember OTM = new OpportunityTeamMember(); OTM.OpportunityId = opp.Id; OTM.TeamMemberRole = 'Analist'; OTM.OpportunityAccessLevel = 'Edit'; List<User> uList = [SELECT Id FROM User Where IsActive = true and ProfileId = '00e4x000000NCX0AAO']; if(uList.size() > 0){ Integer Ran = (integer)(Math.random() * (uList.size())); OTM.UserId = uList[Ran].id; } insert OTM; } } } }When doing the trigger test, there is no error and it completes it well, but in the coverage it stays at 0%, could you help me to know what is wrong in the test or know how I can improve it so that it gives me a percentage of coverage feasible for you to take it as good
This is my Test
@isTest private class SLTestInsertOMT { @isTest(SeeAllData=true) static void TestInsert() { Test.startTest(); Opportunity opp = [select Id, StageName, is_new__c from Opportunity where id_sf__c = '21022600147' LIMIT 1]; if(opp.StageName == 'A' && opp.is_new__c == false) { OpportunityTeamMember otm = new OpportunityTeamMember (OpportunityId= opp.Id, TeamMemberRole = 'Analist', OpportunityAccessLevel = 'Edit', UserId ='0054x000004Hu2xAAC'); insert otm; } System.assert(true); Test.stopTest(); } }
try this following apex class and test class with 100% coverage as well as best practice.
let me know if it helps you and don't forget to make it as best.
Thank You
All Answers
You trigger needs to be bulkified:
1. Don't use the SOQL Queries , DML Statements inside for loop.
2. Don't use the hardcoded record id's in trigger. use the profile name to query the user in below line.
List<User> uList = [SELECT Id FROM User Where IsActive = true and ProfileId = '00e4x000000NCX0AAO']; change to below line
List<User> uList = [ SELECT Id FROM User Where IsActive = true and Profile.Name = 'System Administrator' ];
3. In test class don't use the (SeeAllData=true). Instead of that create test data in test class.
4. Use after contexts in trigger while you performing the actions in related objects.
Please try the below trigger:
Test Class:
I assumed opp.is_new__c field become as false in opp update so i have updated the opp in below test class. Otherwise you can remove the update statement below.
Thanks,
Maharajan.C
Hi Tania,
Please find the Solution. "Apex Test is complete but coverage is 0%"
If you are writing a test class for trigger then you no need to write Test.startTest() and Test.stopTest(); like you have written a trigger on Opportunity Insert then as you run test class it automatically fires the trigger because you have inserted opportunity in test class.
Here is the test class of your code with 100% coverage.
Please let me know it is working or not.
Please mark Best Answer so that other people would take reference from it.
Thanks
try this following apex class and test class with 100% coverage as well as best practice.
let me know if it helps you and don't forget to make it as best.
Thank You
Thank you very much Maharajan C, Suraj Tripathi 47 and veer soni, for your help, I read all your comments, but I did my adjustments and the test regarding what I said veer soni
In the end the test turned out to fail because he sent me the following "Methods defined as TestMethod do not support Web service callouts", but in the coverage it gave me 100%
Regards