+ Start a Discussion
Waqas AliWaqas Ali 

Unit test for Wrapper class

Here is my apex class
public class myclass{
 
    
    public Opportunity o;
    String theId = ApexPages.currentPage().getParameters().get('id');
  
    
   @TestVisible public List<WrapperSObject> records {get; set;}
    public Contract con {get; set;}
    public String selectedValue {get; set;}
  
  public  myclass(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
     
    public PageReference autoRun() {
 
        String theId = ApexPages.currentPage().getParameters().get('id');
         if (theId == null) {
            return null;
        }
    
records = new List<WrapperSObject>();
 for (Contract c : [select id, Name, AccountId, StartDate, Status, ContractNumber from Contract where Status='Activated'  ])
 {
 records.add(new WrapperSObject(c, getOptions(), 'Activated'));
 }
     return null;
          
}
 public PageReference MainMethod() {
 
  try{
     //Create new Oppertunity
             Opportunity opp = new Opportunity(
                     Name = 'TESTACCOUNT_test july' +'Date.today()'
                     ,CloseDate=Date.today()
                     ,StageName= 'Renewal'
                     ,Type='EnerLead Renewal'
                     ,Description='Newly created Oppertunity' 
                );
            insert opp;
   
//try section     
}catch(QueryException e){
}catch(DMLException e){}
    
        return null;        
  }


// To Update contracts 
 public PageReference back() {
       List<Contract> contractToBeUpdate = new List<Contract>();
    for(WrapperSObject wrapper : records){
        wrapper.con.status = wrapper.selectedValue;
        contractToBeUpdate.add(wrapper.con);
    }
    update contractToBeUpdate;
    PageReference pageRef ;    
     return pageRef= MainMethod();
  }
 


@TestVisible public class WrapperSObject {
public Contract con {get; set;}
 public List < SelectOption > options {get; set;}
        public String selectedValue {get; set;}
  
     
    public WrapperSObject( Contract c,List < SelectOption > options, String selectedValue) {
    con=c;
     this.options = options;
     this.selectedValue = selectedValue;
     }
  
  }

public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Activated','Activated'));
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }
  }

here is my test class
@isTest
private class myclassTest {
    private static testMethod void testAutoRun() {

    
        Account acc = new Account(Name='Abce');
  insert acc;
 
  Opportunity  testOppty = new Opportunity();
  testOppty.name='testOppty';
  testOppty.AccountId=acc.id;
  testOppty.CloseDate=System.today();
  testOppty.StageName= 'Closed Won';
  testOppty.Type='EnerLead Renewal';
  testOppty.Description='Newly created Oppertunity';
  insert testOppty;
  
        insert odr;
                  Contract c = new Contract(
                ,StartDate=testOppty.CloseDate
                ,AccountId = testOppty.AccountId
                ,Name = testOppty.Name
                );
            insert c;
  
  
        
        test.startTest();
        
            PageReference pageRef = Page.myclass;
            Test.setCurrentPage(pageRef);
            
            pageRef.getParameters().put('id',testOppty.id);
            ApexPages.StandardController sc = new ApexPages.standardController(testOppty);
            
            myclass  controller = new myclass(sc);
            controller.autoRun();
            controller.back();
            controller.MainMethod();
        test.stopTest();
}
}
How to write wrapper class unit test to get the max code coverage? I am unable to access wrapper class in test class. Guys Help.
 
Best Answer chosen by Waqas Ali
Shashikant SharmaShashikant Sharma
Hi,

Athouhg you do not need to create seaparate methods for covering code for inner class as it should be covered from normal logic test as inner classes are clled from main class method it self.

In case if you want to write a quick test method for code coverage you can create instance of inner class and call proprties and methods from that to cover the code.
 
myClass.WrapperSObject psec = new myClass.WrapperSObject( contractInstance, options, selectedValue );

 

All Answers

Shashikant SharmaShashikant Sharma
Hi,

Athouhg you do not need to create seaparate methods for covering code for inner class as it should be covered from normal logic test as inner classes are clled from main class method it self.

In case if you want to write a quick test method for code coverage you can create instance of inner class and call proprties and methods from that to cover the code.
 
myClass.WrapperSObject psec = new myClass.WrapperSObject( contractInstance, options, selectedValue );

 
This was selected as the best answer
Waqas AliWaqas Ali
but public List < SelectOption > getOptions() {
List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Activated','Activated'));
        options.add(new SelectOption('Expired','Expired'));
        options.add(new SelectOption('LAPSE','Lapse'));
        return options;
    }

is not getting covered why ??
Shashikant SharmaShashikant Sharma
You could call getOptions from instace
 
myClass.WrapperSObject psec = new myClass.WrapperSObject( contractInstance, options, selectedValue );

psec.getOptions();

 
Waqas AliWaqas Ali
Here is the error
Method does not exist or incorrect signature: [myClass.WrapperSObject].getOptions()

Please Help.
Shashikant SharmaShashikant Sharma
Use the instance name instead of directly calling from myClass.WrapperSObject.getOptions()

like 
psec.getOptions();

Where psec is the instance variable.
Waqas AliWaqas Ali
no effect 
Shashikant SharmaShashikant Sharma
Please post your code once.