You need to sign in to do that
Don't have an account?

Unit test for selection method in Wrapper class
Here is my apex class.
Here is my test class
I am unable to cover the Selection getoption() method to increse code coverage. How to write unit test for selection method ?
records.add(new WrapperSObject(c, getOptions(), 'Activated')); //this line is also not getting covered
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')); //this line is not getting covered } return null; } public PageReference MainMethod() { /// other code 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; List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('Activated','Activated')); options.add(new SelectOption('Expired','Expired')); options.add(new SelectOption('LAPSE','Lapse')); 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(); myclass.WrapperSObject psec = new myclass.WrapperSObject( con, options, 'Activated' ); test.stopTest(); } }
I am unable to cover the Selection getoption() method to increse code coverage. How to write unit test for selection method ?
records.add(new WrapperSObject(c, getOptions(), 'Activated')); //this line is also not getting covered
Thanks.
// You can try below code to inc the code coverage. If you can update the contract Status as activated then below wrapper and fun will cover automatic. In mean time you can try below code
List < SelectOption > lst = controller.getOptions();
controller. records.add(new WrapperSObject(c, lst , 'Activated'));
NOTE :- above two line should be come after autoRun method.
Please let us know if this will help u
Thanks,
Amit Chaudhary