Activity Forums Salesforce® Discussions How can I cover the code coverage for multiple conditions in IF statement in Salesforce?

  • How can I cover the code coverage for multiple conditions in IF statement in Salesforce?

    Posted by Achintya on August 20, 2019 at 5:20 am

    Below is the code for class:

    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;
    }
    }

    Deepak replied 5 years, 4 months ago 3 Members · 2 Replies
  • 2 Replies
  • Laveena

    Member
    August 20, 2019 at 5:49 am

    Hi Achintya,

    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);
    }
    }

    Thanks

  • Deepak

    Member
    August 20, 2019 at 12:35 pm

    In certain situation, it is possible that you can cover all if condition in a single method. If it is not covering than make different methods for uncovered conditions.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos