I have a say SomeResolver
whose method resolve
I need to test. The method is creating a new
object which I am trying to mock
. Something like below
public class SomeResolver {
public String resolve() {
// need to mock this
OkHttpClient client = new OkHttpClient().newBuilder().build();
// so that this
Response response = client.newCall(someRequest).execute();
// returns a response I want.
...
}
}
My Test Class SomeResolverTest
can mock final
classes and uses Mockito v3.9.0 but I cannot add the Powermock
dependency
Is there a way I can mock the value returned by new
?
The answer by @Vitor Cavalcanti is good one but for this specific use case, I am better off using the mockwebserver
library provided by OkHttp
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>${okhttp.version}</version>
<scope>test</scope>
</dependency>
Because once I keep adding tests I will need to mock a lot of classes related to the web server and this dependency does that for me.