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

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;
}
}
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;
}
}
Assuming your class name is Questions, you can simply use below test class to get coverage for your getters/setters.
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
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[])
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