0

I am really trying to cover this small class for its code coverage but struggling to do that somehow. Can somebody please help me ?

public with sharing class MultiSelectComponentController {
    public SelectOption[] leftOptions { get; set; }
    public SelectOption[] rightOptions { get; set; }

    public void setOptions(SelectOption[] options, String value) {
        options.clear();
        String[] parts = value.split('&');
        for (Integer i=0; i<parts.size()/2; i++) {
            options.add(new SelectOption(EncodingUtil.urlDecode(parts[i*2], 'UTF-8'), 
              EncodingUtil.urlDecode(parts[(i*2)+1], 'UTF-8')));
        }
    }

    public String leftOptionsHidden { get; set {
           leftOptionsHidden = value;
           setOptions(leftOptions, value);
        }
    }

    public String rightOptionsHidden { get; set {
           rightOptionsHidden = value;
           setOptions(rightOptions, value);
        }
    }
}

The test class which I tried is :

@isTest
public class MultiSelectComponentControllerTest {

    static testMethod void testMultiSelectComponent(){
        Test.startTest();
        SelectOption[] options = new SelectOption[2];
        options.add(new SelectOption('test', 'test'));
        options.add(new SelectOption('value', 'value'));

        MultiSelectComponentController controller = new MultiSelectComponentController();
        controller.rightOptionsHidden = 'Test';
        controller.leftOptionsHidden = 'New Test';
        controller.setOptions(options, 'Game&NewGame');

        System.assert(controller != null);

        Test.stopTest();
    }
}

But its failing and giving me the error...

System.NullPointerException: Attempt to de-reference a null object Class.MultiSelectComponentController.setOptions: line 22, column 1 Class.MultiSelectComponentController.__sfdc_rightOptionsHidden: line 42, column 1 Class.MultiSelectComponentControllerTest.testMultiSelectComponent: line 11, column 1

2 Answers 2

2

In your test class, when the following line is executed

controller.rightOptionsHidden = 'Test';

It goes into the set method for rightOptionsHidden property. After executing the first line, the setOptions method gets called with rightOptions property and the value you are setting for rightOptionsHidden property. Now, the first line inside that method is

options.clear();

and since rightOptions and leftOptions lists are not initialized anywhere, it results in null pointer exception.

You can either add a constructor and initialize the lists or update your test method to initialize lists.

Here is the updated test class

@isTest
public class MultiSelectComponentControllerTest {

    static testMethod void testMultiSelectComponent(){
        Test.startTest();
        SelectOption[] options = new SelectOption[2];
        options.add(new SelectOption('test', 'test'));
        options.add(new SelectOption('value', 'value'));

        MultiSelectComponentController controller = new MultiSelectComponentController();
        controller.rightOptions = new List<SelectOption>();
        controller.rightOptionsHidden = 'Test';
        controller.leftOptions = new List<SelectOption>();
        controller.leftOptionsHidden = 'New Test';
        controller.setOptions(options, 'Game&NewGame');

        System.assert(controller != null);

        Test.stopTest();
    }
}

Hope this helps.

0
0

You should initialize the lists in constructor. Currently they are null.

e.g.:

public MultiSelectComponentController (){
    leftOptions =new list<SelectOption>();
    rightOptions=new list<SelectOption>();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.