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
Peter CowenPeter Cowen 

write test class for Case to CaseComment

I have created a class that links a case with the case comment for a visualforce page but for the life of me I cannot remember how I write a test class for the apex class.

The class I require tests for is:

public class Caselinktocomment
{
    public CaseComment comment { get; set; }
    public String commentText {get; set;}
    public String PublicPrivateAction {get; set;}


    public Caselinktocomment(ApexPages.StandardController stdController) 
    {
        this.comment = new CaseComment(ParentId = stdController.getId());
    }

    public PageReference saveComment()
    {
        insert comment;
        return null;
    }
}

The test Class I have written is:
@istest

Private class Caselinktocommenttests {
    

    static testmethod void createtestdata() {
    Id RecordId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Service_Desk').getRecordTypeId();
        Case tcase = new Case();
        tCase.Status = 'New';
        tCase.Description = 'This ticket has been created as a test';
        tCase.Subject = 'Test Case';
        tCase.RecordTypeId = RecordId;
        tCase.Reason = 'Access Requests';
        tCase.Origin = '';Phone
        
        INSERT tCase;
        
        CaseComment tComment = new CaseComment();
        tComment.ParentId = tCase.Id;
        tComment.CommentBody = 'Test Comment';
        tComment.IsPublished = FALSE;
        tComment.IsNotificationSelected= FALSE;
        Test.startTest();
        INSERT tComment;
        Test.stopTest();
        system.assertEquals(1, [SELECT count() From CaseComment], 'Test Comment');
        }   
}

The code coverage does not seem to increase. Can anyone advise where I have gone wrong. 
Best Answer chosen by Peter Cowen
Foram Rana RForam Rana R
Hi Peter,

I hope you are doing well .....!!
Please use the below code:
@istest

Private class Caselinktocommenttests {
    

    static testmethod void createtestdata() {
        case obj = new case();
        obj.Status = 'New';
        obj.Description = 'This ticket has been created as a test';
        obj.Subject = 'Test Case';
        insert obj;
        
        CaseComment   tComment = new CaseComment();
        tComment.ParentId = obj.Id;
        tComment.CommentBody = 'Some Comment';
        tComment.IsPublished = TRUE;
        
        INSERT tComment;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(obj);
        Caselinktocomment objCaseComment = new Caselinktocomment(sc);
        objCaseComment.commentText = 'Test';
        objCaseComment.PublicPrivateAction = 'Test';
        objCaseComment.saveComment();
        
    }
}

Hope this helps you.
If this helps kindly mark it as solved so that it may help others in the future.

Thanks & Regards,
Foram Rana

All Answers

Foram Rana RForam Rana R
Hi Peter,

I hope you are doing well .....!!
Please use the below code:
@istest

Private class Caselinktocommenttests {
    

    static testmethod void createtestdata() {
        case obj = new case();
        obj.Status = 'New';
        obj.Description = 'This ticket has been created as a test';
        obj.Subject = 'Test Case';
        insert obj;
        
        CaseComment   tComment = new CaseComment();
        tComment.ParentId = obj.Id;
        tComment.CommentBody = 'Some Comment';
        tComment.IsPublished = TRUE;
        
        INSERT tComment;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(obj);
        Caselinktocomment objCaseComment = new Caselinktocomment(sc);
        objCaseComment.commentText = 'Test';
        objCaseComment.PublicPrivateAction = 'Test';
        objCaseComment.saveComment();
        
    }
}

Hope this helps you.
If this helps kindly mark it as solved so that it may help others in the future.

Thanks & Regards,
Foram Rana
This was selected as the best answer
Peter CowenPeter Cowen
Thanks for responding Foram Rana. 

That has worked brillantly!