+ Start a Discussion

How do I get test coverage on my radio buttons?

I have a VF page with 5 radio buttons on.  For the answer I use a radio button (see page item below).  


My question is how do I test this because when I run my test class it doesn't touch this part of the the controllers List<SelectOption> part of the code?


Page item :

<apex:selectradio value="{!question1}" > 
         <apex:selectOptions value="{!QuestionItem}" /> 


Controller item :


// Question radio buttons
public List<SelectOption> getQuestionItem() {
List<SelectOption> options = new List<SelectOption>(); 
options.add(new SelectOption('0','0')); 
options.add(new SelectOption('1','1')); 
options.add(new SelectOption('2','2')); 

return options; 


Test Class :

// Test 1

MyController controller1 = new MyController();
controller1.question1 = 1;



Any help is appretiated.  If I need to add more info just let me know as this is not all of the code just the bits I thought relevant.

Best Answer chosen by Admin (Salesforce Developers) 

You can't really test the radio buttons, as you can't interact with the Visualforce page itself in the test, just the controller.


Therefore you have to set the value as though the user chose a radio button option, as you have done.  You can also test that the getQuestionItem() method returns the expected number of options, but that's about it.