Consider below code snippet:
class XYZ {
public static getFieldABC(){
return new ABC();
}
}
...
CompletableFuture.supplyAsync(() -> XYZ.getFieldABC())
...
I need to unit test this piece of code.
I have a static method getFieldABC
which returns an object of a particular class ABC
. I want to mock this static method call using Mockito.mockStatic and then return mockObject using when()
.
The catch here is that this method call is under CompletableFuture.supplyAsync. Since, its running in a different thread, the call to the static method, instead of calling the mock, its invoking the actual method call.
Assuming the class you want to test looks like this
You can mock statics with Mockito like this: