You need to sign in to do that
Don't have an account?
Waqas Ali
Unit test for Wrapper class
Here is my apex class
here is my test 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.
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.
All Answers
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.
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 ??
Method does not exist or incorrect signature: [myClass.WrapperSObject].getOptions()
Please Help.
like
psec.getOptions();
Where psec is the instance variable.