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
Supriya Bethala 1Supriya 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);
            
    }

}
Raj VakatiRaj Vakati
Try like this ..  you have used offset value wrong in test class

 
@isTest
public class TimelineCaseTest {
    @isTest 
    public static void itShouldBeAbleToGetApplicantCaseListTest1(){
        String applicantId = new TimelineControllerBuilder().save();
        
		Case caseApplicant = new CaseBuilder().withApplicant(applicantId)
                                                .save();
	Case caseApplicant1 = new CaseBuilder().withApplicant(applicantId)
                                                .save();
    
	Case caseApplicant2 = new CaseBuilder().withApplicant(applicantId)
                                                .save();
        
    TimelineCase historyTimelineCase = new TimelineCase();

               
        Test.startTest();
        List<TimelineObject> testFilteredObjects = historyTimelineCase.processTimelineItem(applicantId, 1, 1);
        
        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('standard:case', testFilteredObjects[0].iconName);
      //  System.assertEquals('put the colour in here', testFilteredObjects[0].iconColour);
            
    }

}

 
Supriya Bethala 1Supriya Bethala 1

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

Supriya Bethala 1Supriya Bethala 1
My error is System.AssertException: Assertion Failed: Expected: null, Actual: Supriya B
Raj VakatiRaj Vakati
Why you are test class is failing becase of this line 

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 

 
Raj VakatiRaj Vakati
Why you are getting this error is because of the offset and limit so .. so i updated the code to meet the offset and limit 
 
Supriya Bethala 1Supriya Bethala 1
Changing OFFSET is fine but still i am facing error bcz of we are passing the applicantID as Applicant object that fetching the sql as case object fields, when using System.assertEquals(applicantId, testFilteredObjects[0].actor); as a applicantId is must  retrieve to be name of the person, but here its displaying a id of applicant. so need to be display name instead of id. I am etting error on this line only.