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
SunnyalexSunnyalex 

How to increase code coverage now it is 71% i need to cover 95%

Apex Class
.........................
public class NewAndExistingController {
    public Case Casea { get; public set; }
    public NewAndExistingController() {
        Id id = ApexPages.currentPage().getParameters().get('id');
        casea = (id == null) ? new Case() : 
            [SELECT AccountId,Description,Status,Subject FROM Case WHERE Id = :id];
    }
    public PageReference save() {
        try {
            insert(Casea);
        } catch(System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        PageReference redirectSuccess = new ApexPages.StandardController(Casea).view();
        return (redirectSuccess);
    }
     public PageReference cancel() {
        try {
            insert(Casea);
        } catch(System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        PageReference redirectSuccess = new ApexPages.StandardController(Casea).view();
        return (redirectSuccess);
    }}
...............................................................................................
Test class
..............
@isTest
public class TestClass {
    @isTest
    public static void testMethod1(){

        NewAndExistingController nEC = new NewAndExistingController();
        nEC.save();
        nEC.cancel();
        
        
    }
      
}
Best Answer chosen by Sunnyalex
Maharajan CMaharajan C
Hi Sunny,

Please try the below test class:

I got 100% coverage with below test class.
 
@isTest
public class TestClass{
    @isTest
    public static void testMethod1(){
        NewAndExistingController nEC = new NewAndExistingController();
        nEC.save();
        nEC.cancel();
    }
    
    @isTest
    public static void testMethod2(){
        
        // Add if there is any other fields are mandatory for case creation
        Case ca = new Case(Subject='Test Case', Origin = 'Email');
        insert ca;
        
        apexpages.currentpage().getparameters().put('id', ca.Id);
        NewAndExistingController nEC = new NewAndExistingController();
        nEC.save();
        nEC.cancel();
    }
    
    @isTest
    public static void testMethod3(){
        // Add if there is any other fields are mandatory for case creation
        Case ca = new Case(Subject='Test Case', Origin = 'Email');
        NewAndExistingController nEC = new NewAndExistingController();
        nEC.Casea = ca;
        nEC.cancel();
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Sunny,

Please try the below test class:

I got 100% coverage with below test class.
 
@isTest
public class TestClass{
    @isTest
    public static void testMethod1(){
        NewAndExistingController nEC = new NewAndExistingController();
        nEC.save();
        nEC.cancel();
    }
    
    @isTest
    public static void testMethod2(){
        
        // Add if there is any other fields are mandatory for case creation
        Case ca = new Case(Subject='Test Case', Origin = 'Email');
        insert ca;
        
        apexpages.currentpage().getparameters().put('id', ca.Id);
        NewAndExistingController nEC = new NewAndExistingController();
        nEC.save();
        nEC.cancel();
    }
    
    @isTest
    public static void testMethod3(){
        // Add if there is any other fields are mandatory for case creation
        Case ca = new Case(Subject='Test Case', Origin = 'Email');
        NewAndExistingController nEC = new NewAndExistingController();
        nEC.Casea = ca;
        nEC.cancel();
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
SunnyalexSunnyalex
Thanks  actually i was only stucked in PageReference redirectSuccess = new ApexPages.StandardController(Casea).view();
        return (redirectSuccess);, anyways thanks a lot.
mukesh guptamukesh gupta
Hi SunnyAlex,

Please use below code:-
 
Test Class for Standard Controller
@isTest 
public class TestClass 
{
 static testMethod void testMethod1() 
 {
      Account testAccount = new Account();//Insert Account
        testAccount.Name='Test Account' ;
        insert testAccount;
		
        Contact cont = new Contact();
        cont.FirstName ='Test';
        cont.LastName ='Test';
        cont.accountid =testAccount.id;
        insert cont;
 
   //create case
    Case caseObj = new Case(
    ContactId = cont.Id,
    AccountId = testAccount.Id,
    Status = 'Working',
    Origin = 'Phone',
	Description ='Test case',
	Subject='New Case by Test');

   insert caseObj;

 Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(caseObj);
  NewAndExistingController nEC = new NewAndExistingController(sc);

  PageReference pageRef = Page.yourVFPage; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(caseObj.Id));
  Test.setCurrentPage(pageRef);

  nEC.save();
  nEC.cancel();
  
 Test.StopTest();
 }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh