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
Waqas AliWaqas Ali 

Unit test for selection method in 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')); //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
 
Azhar Aziz GAzhar Aziz G
@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
                ,Status='Activated'
                );
            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();
}
}
Try this one. It may helps..

Thanks.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help u
@isTest
private class myclassTest 
{
    private static testMethod void testAutoRun() 
	{
        Account acc = new Account(Name='Abce');
		insert acc;
		
		Contact cont = new Contact();
		cont.FirstName ='FiestName';
		cont.LastName ='LastName';
		cont.accountid =acc.id;
		insert cont;
 
		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';
			testOppty.Product_Family__c='EnerLead'; 
		insert testOppty;
		
		OpportunityContactRole ocRole = new OpportunityContactRole (ContactId = cont.id, OpportunityId=testOppty.Id, Role='Decision Maker',IsPrimary=TRUE));
		insert ocRole;
  
        //Create the Order
		Order odr = new Order(  
			OpportunityId=testOppty.id
			,AccountId = testOppty.AccountId
			,Name = testOppty.Name
			,EffectiveDate=Date.today()
			,Status='Draft'
		);
		insert odr;
		
		Contract c = new Contract(
			,StartDate=testOppty.CloseDate
			,AccountId = testOppty.AccountId
			,Name = testOppty.Name
			,Status='Activated'
		);
		insert c;
        
        test.startTest();
		 try
		 {
            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();
			// 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'));
		}
		catch(Exception ee)
		{
			
		}	
        test.stopTest();
	}
}

// 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