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
SheffySheffy 

Test Class Help

HI,

 

Please Help me in writing Test Class for this :

 

 

public class CancelCase {

    public String strCaseId = ApexPages.currentPage().getParameters().get('id');
    public String strClosingComments {get; set;}    
    public Case objCancelCase {get; set;}
    public List<CaseComment> objCancelCaseCommentList {get; set;}
    
    public CancelCase(ApexPages.StandardController controller) {
        strClosingComments ='';
        objCancelCaseCommentList = [Select Id, ParentId, IsPublished, CommentBody, CreatedById, CreatedDate, SystemModstamp,
                                    LastModifiedDate, LastModifiedById, IsDeleted, ConnectionReceivedId, ConnectionSentId
                                    From CaseComment where ParentId =:strCaseId ];
       }
    
    public List<SelectOption> getStatus(){
      List<SelectOption> options = new List<SelectOption>();
      Schema.DescribeFieldResult fieldResult = Case.Status.getDescribe();
      options.add(new SelectOption('Cancelled', 'Cancelled'));
      return options;
      }
    
    public PageReference save() {
   
      PageReference createCancelCase;
      try{
       if(strClosingComments==null || strClosingComments==''){
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'You must enter Comments'));
              return null;
       }
    
        CaseComment  objLatestCancelComment = new CaseComment();
        objLatestCancelComment.ParentId = strCaseId;
        objLatestCancelComment.CommentBody= strClosingComments;
        objCancelCaseCommentList.add(objLatestCancelComment);
        upsert objCancelCaseCommentList ;
        
        //strCaseId
        Case caseBeingCancelled = [Select Status From Case where Id=:strCaseId];
        caseBeingCancelled.Status = 'Cancelled';
        update caseBeingCancelled;
        
        createCancelCase = new PageReference('/'+strCaseId);
        createCancelCase.setRedirect(TRUE);
 
        }catch(Exception e){
          }
      return createCancelCase ;

}

}

McFitz13McFitz13
Doze2KatzsDoze2Katzs

It would be hard for someone to write a test class for this when they don't even know how you are calling the class, i.e. from a VisualForce Page or button.

 

Basically, you will need to test the CancelCase method and also the save Method.    Within your save method, you have a 'try catch' section so to get 100% coverage you will need to check both a good save and one that causes an exception.  Also, you have an 'if' statement.  So you want to check the 'strClosingComments' for both when it is empty and when it has a value. 

 

The URL page referenced in the previous post explains aot about testing code.  That was my main tool for learning how to write the testcode.  I agree with the previous reply, stating you should try it first and then if you have problems, someone can assist you.