I have a java class and methods that I want to test with Mockito. These classes in their constructors or in other methods will construct objects and then use those objects in other methods.
I want to mock the behavior of those objects being used. The important thing is that none of these objects are being passed into the class. Rather, they are generated within the class itself. For example lets pretend this is a class I have:
private Client someClient;
public MyClass() {
this.someClient = setupClient();
}
private setupClient() {...}
public methodIWantToTest() {
this.someClient.makeApiCall(String requestString);
// do stuff
}
In my test class and tests I want to be able to do something like:
// test class
...
MyClass classImGoingToTest;
@Test
public void testMethodIWantToTest() {
Mockito.when(Client.class).makeApiCall(Mockito.anyString()).thenDoSomething();
...
}
Of course Mockito needs to make frustratingly difficult to do basic things. So how can this be done in Mockito?
Other answers I read are either of the form "use powermockito" which i want to avoid right now, or "you shouldn't be testing this" which isn't an answer to my question, or they say "well you should pass in the values", which as I explained above is not possible.