Reputation: 1418
I am using JUnit4
and Mockito
for unit test.
Here is the case:
I have a function that needs to be tested, while within this function it contains a network request, and now I want to mock the network request result.
void func(String param1, String param2){
//there would be some validations for the params
//...
//then network request, here I want to mock the result
String result = NetUtils.reqNetwork(param1, param2);
//work with the result
//...
}
Is this possible? Or maybe my test approach is kinds of unreasonable.
Upvotes: 1
Views: 961
Reputation: 652
With only Mockito, there is no way to mock out static method calls. My usual strategy to get around this is to either make these static classes instantiable, or make wrapper classes that are instantiable. Then you can mock those wrappers as usual.
Another option is to use PowerMock, which is built on top of Mockito but provides the capability to mock out these static method calls, along with a host of other things like superclass constructors, final classes, etc.
Upvotes: 1