使用 powermock 测试返回服务的类?
我想使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想要的是注入服务的模拟版本。可以这样完成:
您可以在此处阅读有关绕过封装的更多信息: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:
You can read more about bypassing encapsulation here: http://code.google.com/p/powermock/wiki/BypassEncapsulation