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
ManjusrinuManjusrinu 

Test class for covering boolean variables

Hi,

I have a senario where i need to display phone field on vf page when Leadsource in contact is equal to "other" .I am perfect with page and controller and functionality is working gd .But when comming to test class it is not covering boolean variable . i,e the below highlited line is not covering ...

Can anyone please check it out ?

controller class :

public class ActionFunctionExample {
    public Contact  con{get;set;}
    public Boolean phone{get;set;}
    public ActionFunctionExample() {
        con=new Contact (); 
    }
    public pagereference clickandchange() {
        if(con.LeadSource=='Other') {
           phone=true; 
        }
        else{
            phone= false;
        }
        return null;
    }
}.

Test class :

@istest
public class ActionFunctiontest_TC {
    public static testmethod void testForActiofunction() {
        contact mycon = new contact();
        mycon.LastName='raghu';
        mycon.LeadSource = 'Other';
        mycon.HomePhone= '2483742';
        insert mycon ; 
        ActionFunctionExample ac = new ActionFunctionExample();
        ac.clickandchange();
        
    }
}

Sandesh D GanjareSandesh D Ganjare
Hi  Manjusrinu,

Apex class:
public class newController {
    public Contact  con{get;set;}
    public Boolean phone{get;set;}
    public newController(Contact  c) {
        con=c; 
    }

    public pagereference clickandchange() {
        System.debug('=>'+con.LeadSource);
        if(con.LeadSource=='Other') {
           phone=true; 
        }
        else{
            phone= false;
        }
        return null;
    }
}

Test Class:

@istest
public class newController_Test {
    public static testmethod void testForActiofunction1() {
        
        contact mycon = new contact();
        mycon.LastName='raghu';
        mycon.LeadSource = 'Other';
        mycon.HomePhone= '2483742';
        insert mycon ; 
        Test.startTest();
        newController ac = new newController(mycon);
        ac.clickandchange();
        Test.stopTest();
        
    }
    public static testmethod void testForActiofunction2() {
        
        contact mycon = new contact();
        mycon.LastName='raghu';
        mycon.LeadSource = '';
        mycon.HomePhone= '2483742';
        insert mycon ; 
        Test.startTest();
        newController ac = new newController(mycon);
        ac.clickandchange();
        Test.stopTest();
        
    }
}

If this solved your problem marked it as the best answer. Thanks!
Maharajan CMaharajan C
Hi Manju,

Assign the test contact record  to controller con property.

ActionFunctionExample ac = new ActionFunctionExample();
c.con = mycon;
ac.clickandchange();
@istest
public class ActionFunctiontest_TC {
    public static testmethod void testForActiofunction() {
        contact mycon = new contact();
        mycon.LastName='raghu';
        mycon.LeadSource = 'Other';
        mycon.HomePhone= '2483742';
        insert mycon ; 
        ActionFunctionExample ac = new ActionFunctionExample();
        ac.con = mycon;
        ac.clickandchange();
    }
}

Thanks,
Maharajan.C
​​​​​​​