You need to sign in to do that
Don't have an account?

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?
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?
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
Let us know if this will help you
All Answers
Try below snippet for the test class: Hope this will help.
Thanks,
Aman
still it's not covered " listCMUpdate.add(cm);" part. and it's 70%.
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
Let us know if this will help you