测试中模拟 EJB 注入
每当我想测试使用资源注入的类时,我最终都会包含一个仅在测试中使用的构造函数:
public class A {
@EJB
B b;
// Used in tests to inject EJB mock
protected A(B b) {
this.b = b;
}
public A() {}
// Method that I wish to test
public void foo() {
b.bar();
}
}
是否有另一种模拟资源注入的方法,或者这是要遵循的正确模式?
Whenever I want to test a class which uses resource injection I end up including a constructor that will only be used within the test:
public class A {
@EJB
B b;
// Used in tests to inject EJB mock
protected A(B b) {
this.b = b;
}
public A() {}
// Method that I wish to test
public void foo() {
b.bar();
}
}
Is there another way of mocking resource injection or this is the correct pattern to follow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 easyloss 来达到此效果,它模拟 EJB 注入系统。
另一种方法是在测试中使用反射来设置字段,我有时使用这样的方法:
you could use easy gloss to that effect, it mocks the EJBs injection system.
another way is to set the field using reflexion in your tests, I sometime use something like this :
Eliocs,
如果在接口处输入
B
那么你就不会“仅仅”为测试用例做它;你会允许“B 的行为”的任何替代实现,即使还没有想到它/它们的需要。是的,基本上这是唯一要遵循的模式(据我所知)...所以(无论正确还是错误)你也可以充分利用它;-)
干杯。基思.
Eliocs,
If type
B
where an interface then you wouldn't "just" bo doing it for test-cases; you'd be allowing for any alternative implementations of "B's behaviour", even if the need for it/them hasn't been dreamed-up yet.Yeah, basically that's the only pattern to follow (AFAIK)... so (rightly or wrongly) you may as well make the best of it ;-)
Cheers. Keith.
尽管我依赖包访问,但这当然是一种方法;不提供构造函数注入点,而只是将测试放在与正在测试的 bean 相同的包中。这样,您的测试就可以直接访问该值(假设它不是私有的):
It's certainly one way to do it, although I'd rely on package access; don't provide a constructor injection point, but simply have your test in the same package as the bean being tested. That way, your test can just access the value directly (assuming it's not private):
根据 这篇文章(Mockito 和依赖注入),Mockito 支持注入模拟资源。
您还可以注入多个模拟。只需按照与 B b 相同的方式声明它们即可。
initMocks 部分也可以根据您的需要在每个测试或 BeforeClass 设置方法中完成。
According to this article (Mockito and Dependency Injection), Mockito has support for injecting mocked resources.
You can also inject multiple mocks. Just declare them in the same way as we did for B b.
The initMocks part can also be done in each test or in a BeforeClass setup method depending on your needs.