使用 powermock 测试返回服务的类?

发布于 2024-09-09 08:24:25 字数 556 浏览 1 评论 0原文

我想使用PowerMock测试某些底层服务,但是很复杂。

我想得到你的建议

public interface Service{
  public void someMethod(){
  }
}
public ServiceClient implements Service {
 ...
}
public MyServiceClient {
 public Service getService(){
  return service;
 }
}

我已经编写了一个 ServiceUtil ,它使用 MyServiceClient 来调用并获取服务。

public class ServiceUtil {
 private static service s = MyServiceClient.getService();
 public void updateService(){
  // do some thing with service
 }
}

现在我想测试 ServiceUtil 方法 - updateService。我该怎么做?

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 MyServiceClient 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

超可爱的懒熊 2024-09-16 08:24:25

您可能想要的是注入服务的模拟版本。可以这样完成:

service mockService = Mockito.mock(service.class);
Whitebox.setInternalState(serviceutil.class, mockService);

您可以在此处阅读有关绕过封装的更多信息:http:// code.google.com/p/powermock/wiki/BypassEncapsulation

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文