+ Start a Discussion
VamsiVamsi 

Need Test class code coverage from 86% to 100 %

Hi,

Can someone please help me in covering the test class for the below code and I have tried a lot to cover it to 100% but I am getting 81% 

public with sharing class COCaseComment
{
    public String test2 {get;set;}
    public String test1 {get;set;}
    public string status {get;set;}

    public void save() {
        Id caseId = ApexPages.currentPage().getParameters().get('caseid');
        SavePoint sp = Database.setSavePoint();
        status = 'unsaved';
        try {
            Case c = [SELECT Id,status,xyz1__c, xyz2__c FROM Case WHERE Id = :caseId FOR UPDATE];
            c.Status = 'Closed';
            insert new casecomment(ParentId = caseId, commentbody = 'Question:'+ '\n' + test1 + '\n' + 'solution :'+ '\n'+ test2 , IsPublished = true);
            update c;
            status = 'saved';
        } catch(Exception e) {
            Apexpages.addMessage(new ApexPages.message(Apexpages.Severity.Error, e.getMessage()));
            Database.rollback(sp);
        }
    }
}

Test class 

@isTest
public class TestCustomerOperationsCaseComment
{
    @isTest public static void withcaseid() {
        case c = new
        case (status = 'New', Origin = 'Phone', xyz1__c= 'AMR', xyz2__c = 'Charting Issues');
        insert c;
        case ca = [select id, status, xyz1__c ,xyz2__c from case where status = 'New'];
        Test.setCurrentPage(page.COcaseComment);
        COCaseComment cs = new COCaseComment();
        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:'+ '\n' + test1 + '\n' + 'solution:'+ '\n'+ 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() 
   {
      
            case c = new
            case (status = 'New', Origin = 'Phone', xyz2__c = 'Charting Issues');
            insert c;
            pagereference pr = page.COcaseComment;
            pr.getParameters().put('caseid', c.Id);
            test.setCurrentPage(pr);
            COCaseComment cc = new COCaseComment();
            cc.save();
          System.assert(ApexPages.hasMessages(ApexPages.SEVERITY.Error));
         System.assertEquals('unsaved', cc.status);
    
    }
}



 
Best Answer chosen by Vamsi
FearNoneFearNone
Vamshi,

assign a ca.id that does not exist in the database.
probably like this...
@isTest
public class TestCustomerOperationsCaseComment
{
	...
	case g = [select Status from case where ID = :ca.Id ];
	system.assertEquals('Closed', g.status);
	
	apexpages.currentPage().getparameters().put('caseid', an ID that does not exist or try NULL );
	cs.save();
	List<case> h = [select Status from case where ID = :ca.Id ];
	system.assertEquals(0, h.size());
}

 

All Answers

FearNoneFearNone
Vamshi,

assign a ca.id that does not exist in the database.
probably like this...
@isTest
public class TestCustomerOperationsCaseComment
{
	...
	case g = [select Status from case where ID = :ca.Id ];
	system.assertEquals('Closed', g.status);
	
	apexpages.currentPage().getparameters().put('caseid', an ID that does not exist or try NULL );
	cs.save();
	List<case> h = [select Status from case where ID = :ca.Id ];
	system.assertEquals(0, h.size());
}

 
This was selected as the best answer
VamsiVamsi
Hi Fernand Arioja,

Thank you so much ...!!!! now its covering me 100%
FearNoneFearNone
glad to be of service...