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
Mont MontMont Mont 

Unit test for checkboxes from VF page

I can't understand, why % of my tests is the same.

My code:
 

public class Controller {
    
    public List <Wrapper> wrapperList{get;set;}
    public Boolean bool {get;set;}

 	public Controller(){
        bool = false;
 	}

    public void table(){    
        wrapperList= new list<WrapperClass>();
        
        if(bool == true){
            for(Product2 prod:[SELECT Name FROM Product2 WHERE Name LIKE 'A']){
           		wrapperList.add(new Wrapper(prod, false, 1));
        	} 
        }
}
My test for it is:
 
@isTest
public class controllerTest {
    
    static testMethod void testMyController(){
        test.startTest();

        Product2 productRecord;
        Boolean isSelected;
        Integer quantity;
           
        Controller.Wrapper Wrap = new Controller.Wrapper(productRecord, isSelected, quantity);

        Product2 prod = new Product2(Name = 'someProduct');

        Controller cont = new Controller();
        List<Controller.Wrapper> ourList = new List<Controller.Wrapper>();

        Boolean a = true;
        cont.bool = a;

       if(a == true){
            ourList.add(new Controller.Wrapper(prod, false, 1));
            cont.wrapperList = ourList;
            cont.table();
        }
        test.stopTest();  
   }
}


But I have the same 34%, like if I make it without if. Like this:

@isTest
public class controllerTest {
    
    static testMethod void testMyController(){
        test.startTest();

        Product2 productRecord;
        Boolean isSelected;
        Integer quantity;
           
        Controller.Wrapper Wrap = new Controller.Wrapper(productRecord, isSelected, quantity);

        Controller cont = new Controller();
        List<Controller.Wrapper> ourList = new List<Controller.Wrapper>();

        cont.wrapperList = ourList;
        cont.table();

        test.stopTest();  
   }
}
What is wrong?
Best Answer chosen by Mont Mont
Maharajan CMaharajan C
Hi Mont,

Please try the below update because i have tested the below code and i hvae got the 100%.

==================================

Test Class :

@isTest
 public class controllerTest {
    
    static testMethod void testMyController(){
        // test.startTest();

        Product2 productRecord;
        Boolean isSelected;
        Integer quantity;
           
        productRecord = new Product2(Name = 'A');
        insert productRecord;
        
        isSelected = true;
           
        Controller.Wrapper Wrap = new Controller.Wrapper(productRecord, isSelected, quantity);

        Controller cont = new Controller();
        List<Controller.Wrapper> ourList = new List<Controller.Wrapper>();
        ourList.add(wrap);
        cont.wrapperList = ourList;
        cont.bool = true; 
        cont.table();

        // test.stopTest();  
   }
}

==================================

APEX Class :

public class Controller {

public List <Wrapper> wrapperList{get;set;}
public Boolean bool {get;set;}

public Controller(){
bool = false;
}

public void table(){
wrapperList= new list<Wrapper>();

if(bool == true){
for(Product2 prod:[SELECT Name FROM Product2 WHERE Name LIKE 'A']){
wrapperList.add(new Wrapper(prod, false, 1));
}
}
}

public class Wrapper{

public Product2 prod{get;set;}
public boolean bool{get;set;}
public integer intg{get;set;}

public Wrapper(Product2 p, boolean b,integer i)
{
this.prod = p;
this.bool = b;
this.intg = i;

}

}
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!​


Thanks,
Raj

All Answers

Maharajan CMaharajan C
Hi Mont,

Please try the below update because i have tested the below code and i hvae got the 100%.

==================================

Test Class :

@isTest
 public class controllerTest {
    
    static testMethod void testMyController(){
        // test.startTest();

        Product2 productRecord;
        Boolean isSelected;
        Integer quantity;
           
        productRecord = new Product2(Name = 'A');
        insert productRecord;
        
        isSelected = true;
           
        Controller.Wrapper Wrap = new Controller.Wrapper(productRecord, isSelected, quantity);

        Controller cont = new Controller();
        List<Controller.Wrapper> ourList = new List<Controller.Wrapper>();
        ourList.add(wrap);
        cont.wrapperList = ourList;
        cont.bool = true; 
        cont.table();

        // test.stopTest();  
   }
}

==================================

APEX Class :

public class Controller {

public List <Wrapper> wrapperList{get;set;}
public Boolean bool {get;set;}

public Controller(){
bool = false;
}

public void table(){
wrapperList= new list<Wrapper>();

if(bool == true){
for(Product2 prod:[SELECT Name FROM Product2 WHERE Name LIKE 'A']){
wrapperList.add(new Wrapper(prod, false, 1));
}
}
}

public class Wrapper{

public Product2 prod{get;set;}
public boolean bool{get;set;}
public integer intg{get;set;}

public Wrapper(Product2 p, boolean b,integer i)
{
this.prod = p;
this.bool = b;
this.intg = i;

}

}
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!​


Thanks,
Raj
This was selected as the best answer
Mont MontMont Mont
Thank tou so much! It's work :)