I want to test certain underlying services using PowerMock, but it is complicated.
I would like to get your suggestion
public interface Service{
public void someMethod(){
}
}
public ServiceClient implements Service {
...
}
public MyServiceClient {
public Service getService(){
return service;
}
}
I have written a ServiceUtil which uses MyServiceC开发者_如何转开发lient to call and gets the services.
public class ServiceUtil {
private static service s = MyServiceClient.getService();
public void updateService(){
// do some thing with service
}
}
Now I want to test ServiceUtil method - updateService. How do I do it?
What you probably want is to inject a mock version of the service. This can be done like this:
service mockService = Mockito.mock(service.class);
Whitebox.setInternalState(serviceutil.class, mockService);
You can read more about bypassing encapsulation here: http://code.google.com/p/powermock/wiki/BypassEncapsulation
精彩评论