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
VamsiVamsi 

Can someone please help me in covering the test class

Hi,

I have written a test class that covers 86% leaving out the apexpages.addmessage () in catch block, but majorly it throws the validation exceptions on that object when I try to update a record.(Here I am updating case status to closed and I have some custom fields to be filled out while closing the case).

My test class has 2 methods. 1st method is having the case with all the fields filled this covers all the code except catch block and 2nd method without the required fields while closing the case and even though it doesn't cover my test class to full 100% 

Can someone please provide any suggestions .

In my case I am updating the case status to closed and while closing the case I have 2 custom fields that should not be "null" 1. xyz1__c and 2.xyz2__c (Their are validation rules written on the case obejct and therse throwing me the exceptions if these fields are null while updating the case) I am not sure how to cover the validation exception in catch black in Test class 


Apex controller 
public with sharing class OCaseComment
{
    public String test2 { get; set; }
    public String test1 { get; set; }
    public string status {get;set;}
    public casecomment ca;
    public case c ;
    public ID caseId ;
    
    public pagereference save() 
    {
        
        caseId = ApexPages.currentPage().getParameters().get('caseid');
        system.debug('case id entered' + caseId);
        ca = new casecomment();
        ca.ParentId= caseId;
        ca.commentbody = 'question:'+ test1 + '\n' + 'solution:'+ test2 ;
        ca.IsPublished = true;
        list<case> c = [select Status, xyz1__c, xyz2__c from case where ID=:caseId];
        list<case> cg = new list<case>();
        for(case cd : c)
        {
            cd.Status = 'Closed';
            cg.add(cd);   
        }
       
        try{   
             if(cg.size() > 0 && cg[0].xyz1__c!= null && cg[0].xyz2__c !=null)
        {
            insert ca;
        }
            update cg;
            status = 'saved'; 
            system.debug('status value'+ status);
           
        }catch(DmlException d)
        {
            Apexpages.addMessage(new ApexPages.message(Apexpages.Severity.Error, d.getMessage())) ;  
            status = 'unsaved';
        }
        return null;
    }
    
    
}

Test class 

@isTest
public class TestClassOCaseComment
{
    @isTest public static void withcaseid()
    {
        
            case c = new case(status = 'New',Origin = 'Phone',xyz1__c= 'Stats',xyz2__c= 'Issues');
            insert c;
            case ca = [select id, status,xyz2__c from case where status = 'New'];
            Test.setCurrentPage(page.SCaseComments);
            OCaseComment cs = new OCaseComment();
            cs.Test1=('test1');
            cs.Test2=('test2');
            apexpages.currentPage().getparameters().put('caseid', ca.id);
            if(ca.id != null)
               {
                 cs.save();
               }
       
            casecomment cm = [select commentbody, isPublished from casecomment where parentid =: ca.Id];
            string str = 'Question:'+ cs.test1 +'\n'+ 'Solution:'+ cs.test2 ;
            system.assertEquals(str , cm.CommentBody);
            system.assert(true, cm.ispublished);
            case g = [select Status from case where ID =: ca.Id];
            system.assertEquals('Closed', g.status);
     
     }
    
    @isTest static void caseWithoutproduct()
    {
        try{
            case c = new case(status = 'New',Origin = 'Phone', xyz2__c= 'Issues');
        insert c;
        pagereference pr = page.SCaseComments;
        pr.getParameters().put('caseid',c.Id);
        test.setCurrentPage(pr);
       OCaseComment cc = new OCaseComment();        
            cc.save();  
        }catch (Exception e){     
              System.Assert(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));
            System.Assert(e.getMessage().contains('xyz1__c'));
                               }
}
}
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
@isTest
public class TestClassOCaseComment
{
    @isTest public static void withcaseid()
    {
        
            case c = new case(status = 'New',Origin = 'Phone',xyz1__c= 'Stats',xyz2__c= 'Issues');
            insert c;
            case ca = [select id, status,xyz2__c from case where status = 'New'];
            Test.setCurrentPage(page.SCaseComments);
            OCaseComment cs = new OCaseComment();
            cs.Test1=('test1');
            cs.Test2=('test2');
            apexpages.currentPage().getparameters().put('caseid', ca.id);
            if(ca.id != null)
               {
                 cs.save();
               }
       
            casecomment cm = [select commentbody, isPublished from casecomment where parentid =: ca.Id];
            string str = 'Question:'+ cs.test1 +'\n'+ 'Solution:'+ cs.test2 ;
            system.assertEquals(str , cm.CommentBody);
            system.assert(true, cm.ispublished);
            case g = [select Status from case where ID =: ca.Id];
            system.assertEquals('Closed', g.status);
     
     }
    
    @isTest static void caseWithoutproduct()
    {
        try{
            case c = new case(status = 'New',Origin = 'Phone', xyz2__c= 'Issues');
        insert c;
        pagereference pr = page.SCaseComments;
        pr.getParameters().put('caseid',c.Id);
        test.setCurrentPage(pr);
       OCaseComment cc = new OCaseComment();        
            cc.save();  
        }catch (Exception e){     
              System.Assert(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));
            System.Assert(e.getMessage().contains('xyz1__c'));
                               }
	}

   @isTest static void caseWithoutproduct1()
    {
        try{
            case c = new case(status = 'New',Origin = 'Phone', xyz2__c= 'Issues');
			insert c;
			
			pagereference pr = page.SCaseComments;
			pr.getParameters().put('caseid','invalidID');
			test.setCurrentPage(pr);
			OCaseComment cc = new OCaseComment();        
            cc.save();  
        }
		catch (Exception e)
		{     
        }
	}

}

 
VamsiVamsi
HI Amit,

I tested the above code and it still showing me the 87% code coverage and it doesn't cover the apexpages.addmesage () in catch block.