1

I would like to know how to mock a method which is called in many places in my android project. For instance I have this class:

public class RequestQueue {

    protected RequestQueue mRequestQueue;
    protected Context mCtx;

    public RequestQueue(Context context) {
        ...
    }

    public void addRequestToQueue(BaseRequest request) {
        ...
    }
}

and this one:

public class FakeRequestQueue extends RequestQueue {

    public FakeRequestQueue(Context context, int expectedRetCode) {
        super(context);
        ...
    }

    @Override
    public void addRequestToQueue(BaseRequest request) {
        ...
    }
}

What I want is to replace all the calls to RequestQueue.addRequestToQueue() by FakeRequestQueue.addRequestToQueue() in my whole project. I understand the mock concept but what I found until now is only a way to mock objects locally instantiated. Thank you.

E-Kami
  • 2,529
  • 5
  • 30
  • 50
  • 1
    You could consider to introduce a dependency injection framework, such as Dagger. – rciovati Apr 02 '15 at 10:19
  • Could you please explain your question again? I dont understand what you exactly need.. What do you mean by "to replace all the calls to"? Mockito does not make a method replacement. – akcasoy Apr 08 '15 at 21:56

1 Answers1

0

Using dagger suggested by @rciovati is one solution.

Another solution could be to use robolectric shadow system. A shadow class is like a proxy where you can replace the implementation of your methods. An example can be found at Robolectric shadow not working

Community
  • 1
  • 1
nenick
  • 7,340
  • 3
  • 31
  • 23