You need to sign in to do that
Don't have an account?
LoganUT
Test Class for SOQL cross object trigger
I have built an extensive Opportunity trigger that creates a new Location_Detail__c record, which is the child to Locaiton Master.
I am having a difficult time building a test class to deploy it. I can only achive 10% coverage, and only seem to able to cover the if statement.
Does anyone have any ideas how to test the remainder? I have attahced a section of the trigger and my test class.
Thanks, you guys are the best!
I am having a difficult time building a test class to deploy it. I can only achive 10% coverage, and only seem to able to cover the if statement.
Does anyone have any ideas how to test the remainder? I have attahced a section of the trigger and my test class.
Thanks, you guys are the best!
trigger locStatusOpp on Opportunity (after update, after insert) { List<Location_Detail__c> loctoInsert = new List<Location_Detail__c>(); for (Opportunity opp : Trigger.new) { if (opp.StageName == 'Closed Won' && opp.Location__c == 'Auburn Hills MI' && opp.Category__c != null && opp.Type_of_Charge__c == 'Subscription '){ Location_Master__c locAuburnHillsMI = [SELECT Id FROM Location_Master__c WHERE Name = 'Auburn Hills MI' LIMIT 1]; Location_Detail__c locNewAuburnHillsMI = new Location_Detail__c( Name = opp.Name, Location__c = opp.Location__c, On_List__c = 1, Location_Master__c = locAuburnHillsMI.Id, Vendor_Type__c = opp.Category__c); if(opp.Location__c == 'Auburn Hills MI'){ loctoInsert.add(locNewAuburnHillsMI); } }
@istest private class locStatusOppTEST { @isTest static void locStatusOppTEST(){ List<Location_Detail__c> loctoInsert = new List<Location_Detail__c>(); Opportunity opp = new Opportunity(); opp.Name = 'Test'; opp.AccountId = opp.Id; opp.Type_of_Charge__c = 'Subscription'; opp.Category__c = 'Bartender'; opp.Location__c = 'Des Moines IA'; opp.StageName = 'Closed Won'; opp.CloseDate = date.newInstance(2017, 2, 20); insert opp; } }
Trigger
Test class
Thanks!
Amit
All Answers
Use below code:
Let me know if this helps:
Thanks!
Amit Singh
I assume the problem is coming from the opportunity that you are creating in the test class. You are setting opp.Location__c as 'Des Moines IA' and inserting the opp in the test class. But in the trigger that you wrote in the first If condition you are verifying if the opp.Location__c == 'Auburn Hills MI'. So you are creating an opp in test class whose location does not satisfy the condition you mentioned in the 'If' statement due to it is not going inside if loop and covering the code insite "If" statetement.
Workarounds:
1) Re-visit your logic in the if statement of trigger if that is what you want to check.
OR
2) create another opportunity record in the test method with all the data tat satsfies you if condition( the only missing thing I think is opp.Location__c == 'Auburn Hills MI'). Now you test class should be able to cover all the lines inside the if condition.
Thanks,
Nihar.
Trigger
Test class
Thanks!
Amit
Great catch! The space was causing the issue! Thank you for the second set of eyes on this!
You guys rock!