+ Start a Discussion
Mary Pamishetty 3Mary Pamishetty 3 

test coverage for set method

How to write test class for the below code.

 public String[] MPItemsQues5 { 
        get {
            String[] selected = new List<String>();
            List<SelectOption> sos = this.MPOptionsQues5;
            for(SelectOption s : sos) {
                if (this.oQuestion.Question5__c !=null && this.oQuestion.Question5__c.contains(s.getValue()))
                    selected.add(s.getValue());
            }
            return selected;
        }public set {
            String selectedCheckBox = '';
            for(String s : value) {
                if (selectedCheckBox == '') 
                    selectedCheckBox += s;
                else selectedCheckBox += ';' + s;
            }
            oQuestion.Question5__c = selectedCheckBox;
        }
    } 
Lalit Mistry 21Lalit Mistry 21
Hi Mary,
Assuming your class name is Questions, you can simply use below test class to get coverage for your getters/setters.
@IsTest
private with sharing class QuestionsTest {
	
	private static testMethod void testForGetterSetter{
		//initialize the class. You might also need to set values for other required fields.
		Questions questn = new Questions();
		System.Test.startTest();
		questn.MPItemsQues5 = new List<String>{'Test', 'Class'};
		List<String> selectedQuestions = questn.MPItemsQues5;
		System.Test.stopTest();
	}
}

 
Mary Pamishetty 3Mary Pamishetty 3
Hi Lalit,

I have several get  and set methods but the above code is covering only one get method.For example

 questn.MPItemsQues5 = new List<String>{'Test', 'Class'};
      List<String> selectedQuestions = questn.MPItemsQues5;

 questn.MPItemsQues4 = new List<String>{'Test1', 'Class1'};
      List<String> selectedQuestions1 = questn.MPItemsQues4;



 public String[] MPItemsQues5 { 
        get {
            String[] selected = new List<String>();
            List<SelectOption> sos = this.MPOptionsQues5;
            for(SelectOption s : sos) {
                if (this.oQuestion.Question5__c !=null && this.oQuestion.Question5__c.contains(s.getValue()))
                    selected.add(s.getValue());
            }
            return selected;
        }public set {
            String selectedCheckBox = '';
            for(String s : value) {
                if (selectedCheckBox == '') 
                    selectedCheckBox += s;
                else selectedCheckBox += ';' + s;
            }
            oQuestion.Question5__c = selectedCheckBox;
        }
    } 


is just covering above one.
.
//  is not covering below  code
public String[] MPItemsQues4{ 
        get {
            String[] selected = new List<String>();
            List<SelectOption> sos = this.MPOptionsQues4;
            for(SelectOption s : sos) {
                if (this.oQuestion.Question4__c !=null && this.oQuestion.Question4__c.contains(s.getValue()))
                    selected.add(s.getValue());
            }
            return selected;
        }public set {
            String selectedCheckBox = '';
            for(String s : value) {
                if (selectedCheckBox == '') 
                    selectedCheckBox += s;
                else selectedCheckBox += ';' + s;
            }
            oQuestion.Question4__c = selectedCheckBox;
        }
    } 

Thanks
 
Lalit Mistry 21Lalit Mistry 21
Hi Mary,
In order to cover for rest other getter/setter, you'll need to repeat line no. 8 & 9 for rest other getter and setter too.
so for e.g. to cover for getter/setter of MPItemsQues4, have below code between startTest and stopTest (assuming rest other variables are also of type String[])
questn.MPItemsQues4 = new List<String>{'Test', 'Class'};
List<String> selectedQuestions = questn.MPItemsQues4;
Mary Pamishetty 3Mary Pamishetty 3
Hi Lalit,
I'm getting 'Null reference exception,see the below code.
try{
            List<String> MPItemsQuesList1 = new List<String>{'Gain Weight','Lose Weight','Improve Flexibility'};    
            oQuestion.MPItemsQues1 = MPItemsQuesList1;//I'm getting null reference exception here ,so further code is not excuted
          
              List<String> MPItemsQuesList2 = new List<String>{'Cardio in a club','Run/Jog outside','Weights'};
            oQuestion.MPItemsQues2 = MPItemsQuesList2;
            
            List<String> MPItemsQuesList3 = new List<String>{'Friends','Spouse','Family'};
            oQuestion.MPItemsQues3 = MPItemsQuesList3;
}Catch(Exception e){}


Thanks
Lalit Mistry 21Lalit Mistry 21
Ensure if you have initialized oQuestion before try block.
Mary Pamishetty 3Mary Pamishetty 3
I did.
Lalit Mistry 21Lalit Mistry 21
If you can post your class here to help you out