+ Start a Discussion
LoganUTLoganUT 

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! 
 
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;
    
    }
}

 
Best Answer chosen by LoganUT
Amit Singh 1Amit Singh 1
Use below code for the trigger and test class. Problem is with Type_of_Charge__c == 'Subscription '(here is a space).

Trigger
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);
                    }
         }

Test class
@istest
private class locStatusOppTEST {
    
    @isTest static void locStatusOppTEST(){
        
       	List<Location_Detail__c> loctoInsert  = new List<Location_Detail__c>();
		
		Location_Master__c locMaster = new Location_Master__c(Name="Auburn Hills MI");
		insert locMaster;
        
        Opportunity opp = new Opportunity();
            opp.Name 			  = 'Test';
            opp.AccountId   	  =  opp.Id;
            opp.Type_of_Charge__c = 'Subscription';
            opp.Category__c       = 'Bartender';
            opp.Location__c	      = 'Auburn Hills MI';
            opp.StageName		  = 'Closed Won';
            opp.CloseDate		  = date.newInstance(2017, 2, 20);
            insert opp;
    
    }
}

Thanks!
Amit

All Answers

Amit Singh 1Amit Singh 1
You need to insert Test data for Location_Master__c record into your test class with required fields.

Use below code:
@istest
private class locStatusOppTEST {
    
    @isTest static void locStatusOppTEST(){
        
       	List<Location_Detail__c> loctoInsert  = new List<Location_Detail__c>();
		
		Location_Master__c locMaster = new Location_Master__c(Name="Auburn Hills MI");
		insert locMaster;
        
        Opportunity opp = new Opportunity();
            opp.Name 			  = 'Test';
            opp.AccountId   	  =  opp.Id;
            opp.Type_of_Charge__c = 'Subscription';
            opp.Category__c       = 'Bartender';
            opp.Location__c	      = 'Auburn Hills MI';
            opp.StageName		  = 'Closed Won';
            opp.CloseDate		  = date.newInstance(2017, 2, 20);
            insert opp;
    
    }
}

Let me know if this helps:

Thanks!
Amit Singh
LoganUTLoganUT
Still only recieved 10% coverage. 
Nihar ANihar A
Hello Logan,

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.
 
Amit Singh 1Amit Singh 1
Use below code for the trigger and test class. Problem is with Type_of_Charge__c == 'Subscription '(here is a space).

Trigger
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);
                    }
         }

Test class
@istest
private class locStatusOppTEST {
    
    @isTest static void locStatusOppTEST(){
        
       	List<Location_Detail__c> loctoInsert  = new List<Location_Detail__c>();
		
		Location_Master__c locMaster = new Location_Master__c(Name="Auburn Hills MI");
		insert locMaster;
        
        Opportunity opp = new Opportunity();
            opp.Name 			  = 'Test';
            opp.AccountId   	  =  opp.Id;
            opp.Type_of_Charge__c = 'Subscription';
            opp.Category__c       = 'Bartender';
            opp.Location__c	      = 'Auburn Hills MI';
            opp.StageName		  = 'Closed Won';
            opp.CloseDate		  = date.newInstance(2017, 2, 20);
            insert opp;
    
    }
}

Thanks!
Amit
This was selected as the best answer
LoganUTLoganUT
Amit Singh 1

Great catch! The space was causing the issue! Thank you for the second set of eyes on this!

You guys rock!