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
siva krishna 61siva krishna 61 

How to get 100% code coverage for below code

Apex Class
-------------------
public class Calci {
    
    integer a;
    integer b;
    
    public void cal()
    {
        a=10;
        b=45;
        integer r;
        if(a>b)
        {
            r=a+b;
            system.debug('if a greter than b values should be added'+r);
        }
        else if(a<b)
        {
            r = a-b;
            system.debug('if a is lesser than b values should be subtract'+r);
        }
        else
        {
            system.debug('values are equal');
        }
    }
}


Test Class
--------------
@isTest
public class CalciTest {
    public static testmethod void test(){
       
        Calci c = new Calci();
        c.cal();
    }
}
Best Answer chosen by siva krishna 61
Varun SinghVarun Singh
Hi Siva ,
you can't cover this class 100% because  you are assigning values for a and b in your method.it will cover  either a>b   or a<b
So don't need to assign values in method.just leave it blank and make interger a ,b as public and assign values diffent values when you are calling your method for different if condition a>b(like a=40,b=10) and a<b(a=10,b=40) in your test class.

I am Adding updated code ,you can uder eaisly.

Class
public class Calci {
    
     public integer a;
     public integer b;
    
    public void cal()
    {
        
        integer r;
        if(a>b)
        {
            r=a+b;
            system.debug('if a greter than b values should be added'+r);
        }
        else if(a<b)
        {
            r = a-b;
            system.debug('if a is lesser than b values should be subtract'+r);
        }
        else
        {
            system.debug('values are equal');
        }
    }
}

Test Class:
@isTest
public class CalciTest{
      public static testMethod void methodOne(){
      Calci c=new Calci();
      c.a=45;
          c.b=10;
          c.cal();
          c.a=10;
          c.b=45;
          c.cal();
      }
      
  }

out put Coverage:
User-added image

If Information in informative then make my answer as best answer.

All Answers

Varun SinghVarun Singh
Hi Siva ,
you can't cover this class 100% because  you are assigning values for a and b in your method.it will cover  either a>b   or a<b
So don't need to assign values in method.just leave it blank and make interger a ,b as public and assign values diffent values when you are calling your method for different if condition a>b(like a=40,b=10) and a<b(a=10,b=40) in your test class.

I am Adding updated code ,you can uder eaisly.

Class
public class Calci {
    
     public integer a;
     public integer b;
    
    public void cal()
    {
        
        integer r;
        if(a>b)
        {
            r=a+b;
            system.debug('if a greter than b values should be added'+r);
        }
        else if(a<b)
        {
            r = a-b;
            system.debug('if a is lesser than b values should be subtract'+r);
        }
        else
        {
            system.debug('values are equal');
        }
    }
}

Test Class:
@isTest
public class CalciTest{
      public static testMethod void methodOne(){
      Calci c=new Calci();
      c.a=45;
          c.b=10;
          c.cal();
          c.a=10;
          c.b=45;
          c.cal();
      }
      
  }

out put Coverage:
User-added image

If Information in informative then make my answer as best answer.
This was selected as the best answer
Shruti SShruti S
Your class has lot of flaws. None of the variables are public hence unit testing becomes difficult. Here is the corrected Apex Code and its Unit Test Class - 

Apex Code
public class Calci {
    public Integer cal( Integer a, Integer b ) {
        Integer r;
        
        if( a > b ) {
            r = a + b;
            System.debug( 'if a greter than b values should be added' + r );
            return r;
        }
        else if( a < b ) {
            r = a - b;
            System.debug( 'if a is lesser than b values should be subtract' + r );
            return r;
        }
        else {
            System.debug( 'values are equal' );
            return r;
        }
    }
}

Unit Test
@isTest
public class CalciTest {
    public static testmethod void cal_aGreaterThanB_test() {
        Calci c = new Calci();
        Integer result = c.cal( 20, 10 );
        System.assertEquals( 30, result );
    }
    public static testmethod void cal_aLessThanB_test() {
        Calci c = new Calci();
        Integer result = c.cal( 10, 20 );
        System.assertEquals( -10, result );
    }
    public static testmethod void cal_aEqualToB_test() {
        Calci c = new Calci();
        Integer result = c.cal( 10, 10 );
        System.assertEquals( NULL, result );
    }
}

Feel free to ask if you have any more doubts.
Akshay_DhimanAkshay_Dhiman
Hi Siva,

I hope this Code will help you to write test class

Note:- 
  • If you want you code coverage 100% then you have pass the parameter as an argument of Integer to Execute both if-else condition as I written in below code.
  • I have written two methods in Test class to fill the both scenario of  Apex code that is “ if-else” condition.

Apex Code:
public class Calci1 {
    public static void cal( Integer a, Integer b) // Passing parameters as an argument to make 100% code coverage
    { 
        Integer r;
        if(a>b)
        {
            r=a+b;
            system.debug('if a greater than b values should be added'+r);
        }
        else if(a<b)
        {
            r = a-b;
            system.debug('if a is lesser than b values should be subtract'+r);
        }
        else
        {
            system.debug('values are equal');
        }
    }
}

Test Class:​
@isTest
public class testCalci1 {
@isTest public static void testCalIf(){
       test.startTest(); // Predefined test method to start test
          	Calci1.cal(10,45); // scenario to Execute else condition
       test.stopTest(); // Predefined test method to stop test
    }
@isTest public static void testCalElse(){
       test.startTest();
          	Calci1.cal(45,10);// scenario to Execute if condition

       test.stopTest();
    }    
}


Here is the desired output:-
User-added image

If this answers your query please mark this question as a solved so that it can be filtered out from  unsolved questions.

Regards,
Akshay
 
Varun SinghVarun Singh
@siva ,
Thanks  ,you have any problem  ask to feel free ,i will try to solve you problem.(spnvarun0121@gmail.com)