Search code examples
salesforceapexapex-code

How we can cover the code coverage for multiple conditions in IF statement in Salesforce


I'm trying to write a test class for below class method, but I'm not sure about how we can write tests for multiple conditions in IF statement.

Below is the class method definition :

public class ControllerHelper
{
    public static boolean validate (Case obj)
    {
        if((obj.field1 = 'Yes' || obj.field2 = 'Yes') && checkNullValue(obj.field3))
        ||
        ((obj.field4 = 'Yes' || obj.field5 = 'Yes') && checkNullValue(obj.field6))
       )
       return true;
       else
        {
              return false;
        }
    }

     public static boolean checkNullValue(String value)
            {
                if(value==null || value.trim().length()==0)
                {
                    return true;
                }    
                return false;
            }
}

I did try using Assert & AssertEquals but it's not helping me out.

Any help or suggestion would be really helpful.


Solution

  • You can start with the below code, it will not cover 100%. You have to write a couple of test methods to cover all the fields and logics.

    @isTest
    public class ControllerHelperTest 
    {
        @isTest
        private static void testValidate1()
        {
            ControllerHelper ctrl = new ControllerHelper();
            Boolean isValid = false;
    
            Test.StartTest();
    
            Case case1 = new Case();
            case1.field1 = 'Yes';
            case1.field2 = 'Yes';
            case1.field3 = null;
    
            insert case1;
    
            isValid = ControllerHelper.validate(case1);
    
            Test.stopTest();
    
            System.assert(isValid, true);
        }
    }