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
Andrew  RussoAndrew Russo 

creating a test class for a lightning component apex class.

I am trying to implement a case comment component for communities that was posted in this blog. when I deploy without any text class I get 71% code coverage. what would a test class for this apex class look like? I am just getting into the dev side and I am looking for some help. thank you in advance. 
https://medium.com/@kamatvs/test-66e060ccb91f
/*
* @Description: APEX Server Side Controller.
* Provides methods to fetch case and work with case from Community
*/
public class CommunityCaseDisplayController { 
    
    @AuraEnabled
    public static List<CaseComment> getCaseComments(String caseId) {
        List<CaseComment> caseComments = 
                [SELECT Id, ParentId, IsPublished, CommentBody,    CreatedDate, CreatedBy.Name, LastModifiedDate, LastModifiedById FROM CaseComment where parentId=:caseId order by CreatedDate desc];
return caseComments;        
    }
    
    @AuraEnabled
    public static CaseComment addCaseComment(String caseId, String commentBody) {
        CaseComment caseComment = new CaseComment(ParentId=caseId, CommentBody=commentBody);
        insert caseComment;
return caseComment;        
    }    
}

 
Best Answer chosen by Andrew Russo
AmitSoniAmitSoni
Hi Andrew,

For above APEX class, your test class looks like below:

@isTest
public class CommunityCaseDisplayControllerTest {
    
    @isTest
    static void test_method(){
        
        Account acc = new Account (name='ABC Test');
        insert acc;
        Case c = new case ();
        c.Accountid = acc.Id;
        c.Status = 'New';
        insert c;        
        
        Test.startTest();
        CaseComment caseComment = CommunityCaseDisplayController.addCaseComment(c.Id, 'Testing for Class');
        List<CaseComment> lCC = CommunityCaseDisplayController.getCaseComments(c.Id);
        Test.stopTest();
    }
}

Hope this help. Please mark it as best answer if it resolves your issue.

Thanks!!

All Answers

AmitSoniAmitSoni
Hi Andrew,

For above APEX class, your test class looks like below:

@isTest
public class CommunityCaseDisplayControllerTest {
    
    @isTest
    static void test_method(){
        
        Account acc = new Account (name='ABC Test');
        insert acc;
        Case c = new case ();
        c.Accountid = acc.Id;
        c.Status = 'New';
        insert c;        
        
        Test.startTest();
        CaseComment caseComment = CommunityCaseDisplayController.addCaseComment(c.Id, 'Testing for Class');
        List<CaseComment> lCC = CommunityCaseDisplayController.getCaseComments(c.Id);
        Test.stopTest();
    }
}

Hope this help. Please mark it as best answer if it resolves your issue.

Thanks!!
This was selected as the best answer
Andrew  RussoAndrew Russo
Thank you so much!