0

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?

sushil
  • 127
  • 2
  • 13

1 Answers1

0

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.

Community
  • 1
  • 1
StvnBrkdll
  • 3,924
  • 1
  • 24
  • 31
  • but in my case I have a singleton class and need to mock public method which internally calls private method. So I need to mock public as well as private methods of Singleton class. – sushil Dec 27 '16 at 04:56