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
shravanshravan 

I want 100% code covetage for this test method

Hi,

 

I am able to do test method for this class but I need 100% code coverage for this class.

************* Class *****************

public with sharing class AegisController {

     Account acc;
     Id loggedInUser;
     public boolean errorFlag {get;set;} { errorFlag = false; }
     public AegisController(ApexPages.StandardController controller) {
           acc = (Account)controller.getrecord();
           loggedInUser = UserInfo.getuserId();
     }
     public void RequiredFieldValidation() {
            try {
                    if(acc.Rating == null)
                          errorFlag=true;
            }
            catch(Exception e) {
                    errorFlag=true;
            }
     }
}

 

 

*************** Test Method *******************

@isTest

public class Test_AegisController {
      static testMethod void testAegisController() {
            Account acc = new Account(Name='shravan'); 
            insert acc;
            ApexPages.StandardController sc = new ApexPages.StandardController(acc);
            AegisController aegisController = new AegisController(sc);
            aegisController.RequiredFieldValidation();

            /*
            //-ve test case

            Account acc1 = new Account(); 
            insert acc1;
            ApexPages.StandardController sc1 = new ApexPages.StandardController(acc);
            AegisController aegisController1 = new AegisController(sc);
            aegisController1.RequiredFieldValidation();
            System.debug('****Exception*****');

            */
         }
 }

 

Here code coverage 83%. Plz help me to increase the code coverage.

 

Best Answer chosen by Admin (Salesforce Developers) 
Alok_NagarroAlok_Nagarro

Hi Shravan,

 

The problem is to prevent you to getting 100% coverage  is catch block in RequiredFieldValidation() method. So we have 2 options:

1. Either we can take it out from method.

2. Or define a default constructor in class and call this method without initialize acc (account object). Code is as below for 2nd option -

 

 

public with sharing class AegisController {

     Account acc;
     Id loggedInUser;
     public boolean errorFlag {get;set;} { errorFlag = false; }


     public AegisController(){}    // default constructor


     public AegisController(ApexPages.StandardController controller) {
           acc = (Account)controller.getrecord();
           loggedInUser = UserInfo.getuserId();
     }
     public void RequiredFieldValidation() {
            try {
                    if(acc.Rating == null)
                          errorFlag=true;
            }
            catch(Exception e) {
                    errorFlag=true;
            }
     }
}

 

//-------------test class--------------------

 

@isTest

public class Test_AegisController {

      public static testMethod void testAegisController() {
            Account acc = new Account(Name='shravan');
            insert acc;
            ApexPages.StandardController sc = new ApexPages.StandardController(acc);
            AegisController aegisController = new AegisController(sc);
            aegisController.RequiredFieldValidation();

           
            //-ve test case

            AegisController aegisController1 = new AegisController();
            aegisController1.RequiredFieldValidation();
            System.debug('****Exception*****');

            
         }
 }