1

Class

public with sharing class CaseCommentExtension {
    private final Case caseRec;
    public CaseComment comment {get; set;}

    public CaseCommentExtension(ApexPages.StandardController controller) {
        caseRec = (Case)controller.getRecord();
        comment = new CaseComment();
        comment.parentid = caseRec.id;
    }

    public PageReference addComment() {
        insert comment;

        comment = new CaseComment();
        comment.parentid = caseRec.id;
        return null;
    }   
}

Test Class

@isTest
private class test_caseComments {
    @isTest public static void insertComment(){

        //create test case 
        String caseSubject = 'From Case: my test';
        String caseDesc = 'my test description';
            Case c = new Case();
             c.subject= caseSubject;
             c.Description = caseDesc;
            insert c;

            System.debug(c);

            CaseComment cmt = new CaseComment();
            cmt.parentid = c.Id;
            insert cmt;

            System.debug(c.Id);
            System.debug(cmt.ParentId);  

            CaseCommentExtension.addComment(cmt);

    }
}
  • error is on which line? – Prajith Feb 8 '16 at 17:18
  • The 'addComment' method doesn't take any parameters as per your definition, but in the test class it is shown to take arguments. Thanks – Prajith Feb 8 '16 at 17:28
3

There are two reasons you cannot call CaseCommentExtension.addComment(cmt):

  • The method is not static
  • The method does not accept any parameters

So to properly test it, you need to instantiate an extension and call the method with no parameters.

Case record; // setup, insert, etc.
ApexPages.StandardController controller = new ApexPages.StandardController(record);
CaseCommentExtension extension = new CaseCommentExtension(controller);

Test.startTest();
    extension.comment = something;
    extension.addComment();
Test.stopTest();

system.assert(somethingElse, 'Message');
3

This one is the problematic line in your Test Class -

CaseCommentExtension.addComment(cmt);

In your original class the method doesn't take any input parameters but you are passing it in your test class

public PageReference addComment() {
        insert comment;

        comment = new CaseComment();
        comment.parentid = caseRec.id;
        return null;
    } 
  • Ok perfect, thanks for the clarification. How would you approach fixing it? I cant just pass the field values as a parameter. should I just append insert cmt instead of passing it as a parameter? – Allen Mann Feb 8 '16 at 18:05
  • Is your Apex class working as per your business requirement? If yes, then why change it for the sake of test class? Just call the method as it's defined in your class. CaseCommentExtension.addComment(); – Jarvis Feb 8 '16 at 18:16
  • Sorry I wasnt meaning I would change the class. Just trying to get the test class to pass it now. Just using CaseCommentExtension.addComment(); gives me the same error. – Allen Mann Feb 8 '16 at 18:26
  • Okay, as addComment() is an instance method, you can't call directly from the class, you have to instantiate the object then call your method. – Jarvis Feb 8 '16 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.