You need to sign in to do that
Don't have an account?

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