Test method of Singleton object using Powermock

505 views Asked by At

I would like to test a public method1 as well as mock the private method createJSON of Singleton class.

public class SingletonClass {
    private static SingletonClass singletonInstance = new SingletonClass();
    private SingletonClass() {
    }

    public static SingletonClass getInstance() {
        return singletonInstance;
    }

    public JSONObject method1(int id, String str)
        throws JSONException {
        JSONObject loginJSON = createJSON(id, str);
        return loginJSON;
    }

    private JSONObject createJSON(int id, String str){
        return new JSONObject().put("id", id).put("str", str);
    }

}

Could anyone help on this?

1

There are 1 answers

1
StvnBrkdll On

It sounds like you need a partial mock. A partial mock will allow you to mock a subset of the methods of the class you are working with, but not others.

This SO post explains how to use partial mocks.