You need to sign in to do that
Don't have an account?
Supriya Bethala 1
How to write a test class for wrapper class below apex class
I have tried to write test class below apex class, its below in that code i can acheive 100% apex test coverage, but in test class
// i need to bring the case created name here to match the actual values. but getting null values.
- System.assertEquals(applicantId, testFilteredObjects[0].actor); this line i am getting expected values as null- thats why my test class failed, please help me sort out of this,
Please let me know if you need further information.
Apex class - TimelineCase
public class TimelineCase extends TimelineObject implements HisTimelineObject{
public List<TimelineObject> processTimelineItem(id recordId, Integer pageNo, Integer pageSize){
List<TimelineObject> wrappedCase = new List<TimelineObject>();
List<Case> t = [SELECT Id, Subject, AccountId, CreatedDate, CreatedBy.Name
FROM Case
WHERE Applicant__c = :recordId
LIMIT :pageSize
OFFSET :pageNo];
if(t != null){
for(Integer i = 0, CaseSize = t.size(); i < CaseSize; i++){
wrappedCase.add(new TimelineObject().setActor(t[i].CreatedBy.Name)
.setHeader(t[i].Subject)
.setDate(t[i].CreatedDate.format())
.setIconName('standard:case')
.setIconColour('put the colour in here'));
}
}
return wrappedCase;
}
}
Test class
@isTest
public class TimelineCaseTest {
@isTest
public static void itShouldBeAbleToGetApplicantCaseListTest1(){
String applicantId = new TimelineControllerBuilder().save();
Case caseApplicant = new CaseBuilder().withApplicant(applicantId)
.save();
TimelineCase historyTimelineCase = new TimelineCase();
Test.startTest();
List<TimelineObject> testFilteredObjects = historyTimelineCase.processTimelineItem(applicantId, 1, 10);
Test.stopTest();
System.debug('Case result' + caseApplicant.CreatedBy.Name + testFilteredObjects[0].actor);
System.assertEquals(1, testFilteredObjects.size());
//Expected values showing null, actual values get correct
System.assertEquals(applicantId, testFilteredObjects[0].actor);
System.assertEquals(, testFilteredObjects[0].itemDate);
System.assertEquals('standard:case', testFilteredObjects[0].iconName);
System.assertEquals('put the colour in here', testFilteredObjects[0].iconColour);
}
}
// i need to bring the case created name here to match the actual values. but getting null values.
- System.assertEquals(applicantId, testFilteredObjects[0].actor); this line i am getting expected values as null- thats why my test class failed, please help me sort out of this,
Please let me know if you need further information.
Apex class - TimelineCase
public class TimelineCase extends TimelineObject implements HisTimelineObject{
public List<TimelineObject> processTimelineItem(id recordId, Integer pageNo, Integer pageSize){
List<TimelineObject> wrappedCase = new List<TimelineObject>();
List<Case> t = [SELECT Id, Subject, AccountId, CreatedDate, CreatedBy.Name
FROM Case
WHERE Applicant__c = :recordId
LIMIT :pageSize
OFFSET :pageNo];
if(t != null){
for(Integer i = 0, CaseSize = t.size(); i < CaseSize; i++){
wrappedCase.add(new TimelineObject().setActor(t[i].CreatedBy.Name)
.setHeader(t[i].Subject)
.setDate(t[i].CreatedDate.format())
.setIconName('standard:case')
.setIconColour('put the colour in here'));
}
}
return wrappedCase;
}
}
Test class
@isTest
public class TimelineCaseTest {
@isTest
public static void itShouldBeAbleToGetApplicantCaseListTest1(){
String applicantId = new TimelineControllerBuilder().save();
Case caseApplicant = new CaseBuilder().withApplicant(applicantId)
.save();
TimelineCase historyTimelineCase = new TimelineCase();
Test.startTest();
List<TimelineObject> testFilteredObjects = historyTimelineCase.processTimelineItem(applicantId, 1, 10);
Test.stopTest();
System.debug('Case result' + caseApplicant.CreatedBy.Name + testFilteredObjects[0].actor);
System.assertEquals(1, testFilteredObjects.size());
//Expected values showing null, actual values get correct
System.assertEquals(applicantId, testFilteredObjects[0].actor);
System.assertEquals(, testFilteredObjects[0].itemDate);
System.assertEquals('standard:case', testFilteredObjects[0].iconName);
System.assertEquals('put the colour in here', testFilteredObjects[0].iconColour);
}
}
Hi Raj,
I am not sure, what you tried in this, creating new case record caseApplicant1,caseApplicant2 is not solved actually, Could you please try another way. please.
I Expecting solution is case record createdby.name and createdDate should be System.assertEquals(CaseCreatedDate&time,testFilteredObjects[0].itemDate);// CaseCreatedDate&time should be display from case record
System.assertEquals(CaseCreatedBy.Name, testFilteredObjects[0].actor);// CaseCreatedBy.Name should be display as whoever created the case record should be visible.
Even i can provide you TimelineObject class for more information.
global virtual class TimelineObject {
@AuraEnabled
public String actor;
@AuraEnabled
public String header;
@AuraEnabled
public String iconName;
@AuraEnabled
public String iconColour;
@AuraEnabled
public String itemDate;
public TimelineObject setActor(String newActor){
this.actor = newActor;
return this;
}
public TimelineObject setHeader(String newHeader){
this.header = newHeader;
return this;
}
public virtual TimelineObject setIconName(String newIconName){
this.iconName = newIconName;
return this;
}
public virtual TimelineObject setIconColour(String newIconColour){
this.iconColour = newIconColour;
return this;
}
public TimelineObject setDate(String newDate){
this.itemDate = newDate;
return this;
}
}
List<TimelineObject> testFilteredObjects = historyTimelineCase.processTimelineItem(applicantId, 1, 10);
so i changed my code to meet the conditions to
List<TimelineObject> testFilteredObjects = historyTimelineCase.processTimelineItem(applicantId, 1, 1);
Did you run test class with the above change and let me know