开发者

testing the class that returns the service using powermock?

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜