1

In wrapper class, there is a boolean variable named isSelected which I need to make true to increase coverage of a method. The constructor of the class always initializes the value to false. I am not able to change the value to true in test class because the list of wrapper class that is needed is returned by another method.

Check the code below and follow the comments to see the problem:

public class BIContractsDetailCon {
    //Wrapper Class
    public class BIContractWrapper {
        public BI_Contracts__c biContract {get;set;}
        public Boolean isSelected {get;set;}  //The field of interest
        //constructor
        public BIContractWrapper(BI_Contracts__c biContract){
            this.biContract = biContract;
            isSelected = false;  //Constructor initializes it to false
        }
    } 

    public PageReference createRenewalOpportunity(){
    List<BIContractWrapper> biConWList = new List<BIContractWrapper>(); //List of wrapper class
    biContractWrapperlist=getBIContractWrappers(); //The function definition is pasted below
        for(BIContractWrapper w:biContractWrapperlist){
            if(w.isSelected){  //I need to make this true
                biConWlist.add(w);  //I want to cover this line
            }
        }
        //Other code
    }

    public List<BIContractWrapper> getBIContractWrappers(){
         //Other code
         for(BI_Contracts__c r: lbc){
                BIContractWrapper w = new BIContractWrapper(r);//Create wrapper class object
                biConWrapperlist.add(w);  //Add to list of wrapper class objects
        }
      return biConWrapperlist ;   //Return the list. Problem is, isSelected is always false for all objects in the list
    }
}

isSelected is not made true anywhere in the class. Please guide. Thank You!

Updates:

Here is the test class that I tried. It didn't work because making isSelected=true is of no use as its value is overwritten in the class

public static testMethod void TestBiContracts1(){

    BI_Contracts__c bicon1 = new BI_Contracts__c(Contract_Number__c='1001',Delivery_Method__c='AP Archive',Created_On__c=system.today(),
                                                    MDM_Account_ID__c='10008478',MDM_Account_Name__c='Berkeley Independent',Customer__c=account.Id,
                                                    Bill_to_MDM_ID__c='10103964',Bill_to_customer__c='2K Sports',
                                                    License_to_MDM_ID__c='10108877',License_to_Name__c='20th Century Fox',
                                                    Ship_to_MDM_ID__c='10110984',Ship_To_Name__c='12 yard Productions');

    BIContractsDetailCon.BIContractWrapper biconwrapper1 = new BIContractsDetailCon.BIContractWrapper(bicon1);

    BIContractsDetailCon biConDet = new BIContractsDetailCon();
    biConDet.biContractWrapperlist.add(biconwrapper1);

    for(BIContractsDetailCon.BIContractWrapper w: biConDet.biContractWrapperlist){
        w.isSelected=true;
    }
    biConDet.createRenewalOpportunity();
}
4
  • What you have tried so far? Can you add the test class here? – rahul gawale Mar 2 '20 at 9:57
  • @rahulgawale I have added the test class. Thanks! – SFdev Mar 2 '20 at 10:14
  • When is the isSelected is marked to true in your code? Does it happen from UI/VF page? – rahul gawale Mar 2 '20 at 10:45
  • @rahulgawale Yes – SFdev Mar 2 '20 at 12:17
1

You can make the isSelected true by default just for the test environment.

public BIContractWrapper(BI_Contracts__c biContract){
    this.biContract = biContract;
    isSelected = Test.isRunningTest();
}

This way isSelected will be always true in just the Test transactions and false for others.

2
  • I was looking to cover it without changing the code of the class. If there is no other option then I'll go for it. Thanks a lot! – SFdev Mar 2 '20 at 12:34
  • 1
    @Koustubh I am not sure if there is another way around. – rahul gawale Mar 2 '20 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.