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
Nishit MistryyyNishit Mistryyy 

test class for diplaying date for milestone

I have a class that selects target date and case id from an object CaseMilestone and then it puts it into a list and then it mappes the target date as the milestone date... I want to write a test class for it but my test class only covers 57% of the code... It is unable to enter the IF condition present in the main class

THE CODE FOR THE CLASS IS:
public class DisplayCaseMilestoneDateCtrl {
    @AuraEnabled(cacheable=true)
    public static string getCaseMilestoneDate(String caseId){
        List<CaseMilestone> cmList = [SELECT targetDate,caseId FROM CaseMilestone where caseId =: caseId];
        if(cmList != null && cmList.size() > 0){
            DateTime dt = cmList[0].targetDate;
            string milestoneDate = dt.format('d-MMM-yyyy hh:mm a');
            return milestoneDate;
        }
        else{
            return 'No milestone date to show.';
        }
    }
}

 

And THE CODE FOR THE TEST CLASS WITH 57% code coverage is:
@isTest
public class DisplayCaseMilestoneDateCtrlTest {
    static testmethod void testmethod1() {
        List<Id> idss = new List<Id>();
        CaseMilestone cms = new CaseMilestone();
        idss.add(cms.CaseId);
        Test.startTest();
        DisplayCaseMilestoneDateCtrl.getCaseMilestoneDate('500N000000J0MkfIAF');
        Test.stopTest();
        
    }
    
}

Vivek Garg 13Vivek Garg 13
You must provide test data and make sure that it meets the requirements for entering the if condition in the main class in order to increase the code coverage for your test class. A modified version of your test class that addresses the IF condition is provided here:
 
Java

@isTest
public class DisplayCaseMilestoneDateCtrlTest {
    static testmethod void testmethod1() {
        // Create test data
        CaseMilestone cms = new CaseMilestone();
        cms.CaseId = '500N000000J0MkfIAF';
        cms.TargetDate = DateTime.now();
        insert cms;

        Test.startTest();
        String milestoneDate = DisplayCaseMilestoneDateCtrl.getCaseMilestoneDate(cms.CaseId);
        Test.stopTest();

        // Verify the results
        System.assertEquals(cms.TargetDate.format('d-MMM-yyyy hh:mm a'), milestoneDate);
    }
}

We start by creating a "CaseMilestone" record in this revised test class with a matching "CaseId" and a non-null "TargetDate." You may enter the if condition by introducing this record to make sure that the 'cmList' in the main class is not null and has a size larger than 0, allowing it to do so.

Then, using the relevant "CaseId," we invoke the "getCaseMilestoneDate" function. Finally, we claim that the formatted "TargetDate" from the "CaseMilestone" record matches the returned "milestoneDate." This claim confirms that the procedure returns the anticipated date.

With these adjustments, the test should now cover the Salesforce admin (https://www.igmguru.com/salesforce/salesforce-administration-training/) main class's if condition, increasing code coverage.