从构造函数参数模拟对象
我有以下类布局:
public class Service {
ServiceHelper helper;
...class methods...
}
public class ServiceHelper {
Foo foo;
Bar bar;
...class methods...
}
我正在为服务创建单元测试,但是我想将ServiceHelper用作“实时”类,但是ServiceHelper内的构造函数参数要被嘲笑。有没有办法通过Mockito实现这一目标?
I have the following class layouts:
public class Service {
ServiceHelper helper;
...class methods...
}
public class ServiceHelper {
Foo foo;
Bar bar;
...class methods...
}
I am creating a unit test for Service, but I want to use ServiceHelper as a "live" class, but the constructor parameters inside the ServiceHelper to be mocked. Is there a way to achieve this via Mockito?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的榜样不是一个好习惯。
单元测试
应尽可能小。单元测试的目的是隔离程序的每个部分,并表明各个部分是正确的。无论如何,您仍然可以尝试这种方法。我希望它有帮助。
和测试类:
I think your example is not a good practice.
Unit testing
should be as small as possible. The goal of unit testing is to isolate each part of the program and show that the individual parts are correct.Anyway, you still can try this approach. I hope it helps.
And the test class :