You need to sign in to do that
Don't have an account?
Rajesh SFDC
How to write test class for both 'If 'and 'Else' statements
trigger HistoryTracking on Case (before update)
{
Map<string, Schema.SobjectField> caseFields = Schema.SObjectType.Case.fields.getMap();
case caseold=trigger.old[0];
For (Case cs : Trigger.new)
{
for (string fieldName : caseFields.keySet())
{
if ( cs.get(fieldName) != Trigger.oldMap.get(cs.id).get(fieldName))
{
string oldvalue = string.valueOf(caseold.get(fieldName));
string newvalue= string.valueOf(cs.get(fieldName));
string parentoldvalue = string.valueOf(caseold.get('ParentId'));
string parentnewvalue= string.valueOf(cs.get('ParentId'));
if(fieldName != 'Description')
cs.History_Tracking__c = 'Changed '+fieldName +' From '+oldvalue +' to '+ newvalue; ------------->this line doen't cover the code coverage
else
cs.History_Tracking__c = 'Changed On Long Field' ; ------------->this line doen't cover the code coverage
if(parentoldvalue != null && parentnewvalue== null )
{
cs.CheckParent__c='RemoveParent'; ------------>this line doen't cover the code coverage
system.debug('______________checkparentid+++++++++++++++++++'+cs.CheckParent__c);
}
else if( parentnewvalue != null)
{
cs.CheckParent__c='';
}
break;
} }
}}
test class
=============
@isTest(seeAllData=true)
private class Testcasehistory
{
static testMethod void updatefields()
{
list<Case> cslist=new list<case>();
case cs=new case();
cs.Origin='Web';
cs.Reason='other';
cs.Priority='Medium';
cs.Status='New';
cs.Type='Other';
cs.Subject='ESTDeveloper';
cs.Priority='1 - Critical';
cs.TFS_WorkItem_Type__c='Bug';
cs.Description='changedSFDCTOTFS';
cs.ParentId=cs.id;
// System.assertEquals(cs.Description,' cs.History_Tracking__c');
// cs.History_Tracking__c='Changed On Long Field';
if(cs.ParentId == null)
{
cs.CheckParent__c='RemoveParent';
}
else if(cs.ParentId != null)
{
cs.CheckParent__c='';
}
// cs.Origin='Email';
cslist.add(cs);
test.startTest();
insert cslist;
cs.Origin='Email';
update cslist;
test.stopTest();
}
}
{
Map<string, Schema.SobjectField> caseFields = Schema.SObjectType.Case.fields.getMap();
case caseold=trigger.old[0];
For (Case cs : Trigger.new)
{
for (string fieldName : caseFields.keySet())
{
if ( cs.get(fieldName) != Trigger.oldMap.get(cs.id).get(fieldName))
{
string oldvalue = string.valueOf(caseold.get(fieldName));
string newvalue= string.valueOf(cs.get(fieldName));
string parentoldvalue = string.valueOf(caseold.get('ParentId'));
string parentnewvalue= string.valueOf(cs.get('ParentId'));
if(fieldName != 'Description')
cs.History_Tracking__c = 'Changed '+fieldName +' From '+oldvalue +' to '+ newvalue; ------------->this line doen't cover the code coverage
else
cs.History_Tracking__c = 'Changed On Long Field' ; ------------->this line doen't cover the code coverage
if(parentoldvalue != null && parentnewvalue== null )
{
cs.CheckParent__c='RemoveParent'; ------------>this line doen't cover the code coverage
system.debug('______________checkparentid+++++++++++++++++++'+cs.CheckParent__c);
}
else if( parentnewvalue != null)
{
cs.CheckParent__c='';
}
break;
} }
}}
test class
=============
@isTest(seeAllData=true)
private class Testcasehistory
{
static testMethod void updatefields()
{
list<Case> cslist=new list<case>();
case cs=new case();
cs.Origin='Web';
cs.Reason='other';
cs.Priority='Medium';
cs.Status='New';
cs.Type='Other';
cs.Subject='ESTDeveloper';
cs.Priority='1 - Critical';
cs.TFS_WorkItem_Type__c='Bug';
cs.Description='changedSFDCTOTFS';
cs.ParentId=cs.id;
// System.assertEquals(cs.Description,' cs.History_Tracking__c');
// cs.History_Tracking__c='Changed On Long Field';
if(cs.ParentId == null)
{
cs.CheckParent__c='RemoveParent';
}
else if(cs.ParentId != null)
{
cs.CheckParent__c='';
}
// cs.Origin='Email';
cslist.add(cs);
test.startTest();
insert cslist;
cs.Origin='Email';
update cslist;
test.stopTest();
}
}
private class Testcasehistory
{
static testMethod void updatefields()
{
list<Case> cslist=new list<case>();
case cs=new case();
cs.Origin='Web';
cs.Reason='other';
cs.Priority='Medium';
cs.Status='New';
cs.Type='Other';
cs.Subject='ESTDeveloper';
cs.Priority='1 - Critical';
cs.TFS_WorkItem_Type__c='Bug';
cs.Description='changedSFDCTOTFS';
cs.ParentId=cs.id;
string parentoldvalue = cs.ParentId;
string parentnewvalue=null;
if(parentoldvalue != null && parentnewvalue== null )
{
cs.CheckParent__c='RemoveParent';
}
cslist.add(cs);
test.startTest();
insert cslist;
// System.AssertEquals('Description','Description');
cs.Origin='Email';
update cslist;
test.stopTest();
}
static testMethod void updatefields1()
{
list<Case> cslist=new list<case>();
case cs=new case();
cs.Origin='Web';
cs.Reason='other';
cs.Priority='Medium';
cs.Status='New';
cs.Type='Other';
cs.Subject='ESTDeveloper';
cs.Priority='1 - Critical';
cs.TFS_WorkItem_Type__c='Bug';
cs.ParentId=cs.id;
string parentnewvalue1= cs.ParentId;
if( parentnewvalue1 != null)
{
cs.CheckParent__c='';
}
cslist.add(cs);
test.startTest();
insert cslist;
// String fieldName = 'Description';
if( parentnewvalue1 != null)
{
cs.CheckParent__c='';
}
cs.Origin='Email';
update cslist;
test.stopTest();
}
}
I Changed like dis doesn't covering