function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Moloy Biswas 17Moloy Biswas 17 

Need help to increase test coverage

Trigger is as below:
trigger CMbeforeUpdate on CM__c (before Update) {
    List<CM__c> listCMUpdate = new List<CM__c>();
    for(CM__c cm : trigger.new) {
        if(cm.CM_Reason__c != trigger.oldMap.get(cm.id).CM__c 
            || cm.Location__c != trigger.oldMap.get(cm.id).Location__c
            
            || cm.Credit_LOB__c != trigger.oldMap.get(cm.id).Credit_LOB__c) {
            listCMUpdate.add(cm);
        }
    }
    if( listCMUpdate.size() > 0)
    { 
        CM_Handler.updateReasonCategoryCode(listCMUpdate);
        CM_Handler.CMapproverdetails(listCMUpdate);
    }    
}
The test class is:
@isTest
public class CMbeforeUpdatetest {
static testMethod void testCheck() {
   CM__c cm= new CM__c();
    cm.name = 'test';
    cm.CM_Reason__c = 'test';
    cm.Location__c = 'test';
 
  
    insert cm; 
    
    cm.Location__c = 'test123';
    
    update cm;
   
  }
}
the coverage is 70%. Need to cover " listCMUpdate.add(cm);" part, How to do that?
Best Answer chosen by Moloy Biswas 17
Amit Chaudhary 8Amit Chaudhary 8
Hi Moloy,

If below Trigger code is fine ?

if(cm.CM_Reason__c != trigger.oldMap.get(cm.id).CM__c 

I guess it should be like below

if(cm.CM_Reason__c != trigger.oldMap.get(cm.id).CM_Reason__c

Please try below test class
@isTest
public class CMbeforeUpdatetest {
	static testMethod void testCheck() 
	{
	
		CM__c cm= new CM__c();
		cm.name = 'test';
		cm.CM_Reason__c = 'test';
		cm.Location__c = 'test';
		insert cm; 

		Test.StatTest();
			cm.CM_Reason__c = 'test123';
			cm.Location__c = 'test123';
			update cm;
		Test.StopTest();
		
		
	}
}

Let us know if this will help you
 

All Answers

Aman MalikAman Malik
Hi,
Try below snippet for the test class:
@isTest
public class CMbeforeUpdatetest {
static testMethod void testCheck() {
   CM__c cm= new CM__c();
    cm.name = 'test';
    cm.CM_Reason__c = 'test';
    cm.Location__c = 'test';
    insert cm; 
    
    CM__c cm1= new CM__c(id=cm.id, Location__c = 'test123');
    update cm1;
   
  }
}
Hope this will help.

Thanks,
Aman
 
Moloy Biswas 17Moloy Biswas 17
@Aman Malik
still it's not covered " listCMUpdate.add(cm);" part. and it's 70%.
Amit Chaudhary 8Amit Chaudhary 8
Hi Moloy,

If below Trigger code is fine ?

if(cm.CM_Reason__c != trigger.oldMap.get(cm.id).CM__c 

I guess it should be like below

if(cm.CM_Reason__c != trigger.oldMap.get(cm.id).CM_Reason__c

Please try below test class
@isTest
public class CMbeforeUpdatetest {
	static testMethod void testCheck() 
	{
	
		CM__c cm= new CM__c();
		cm.name = 'test';
		cm.CM_Reason__c = 'test';
		cm.Location__c = 'test';
		insert cm; 

		Test.StatTest();
			cm.CM_Reason__c = 'test123';
			cm.Location__c = 'test123';
			update cm;
		Test.StopTest();
		
		
	}
}

Let us know if this will help you
 
This was selected as the best answer