Weld (CDI):我应该将配置的仅测试 beans.xml 放在哪里?
我的 web 应用程序在 src/main/resources/META-INF
下有一个非空的生产 beans.xml
。 现在,对于我的测试,我需要用替代品替换 1 个 bean。
我应该把这个只包含这个而仅包含此内容的测试 beans.xml
放在哪里?
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<alternatives>
<class>...MyTestReplacement</class>
</alternatives>
</beans>
我在 src/test/resources/META-INF 下尝试过,但被忽略了。我正在使用 arquillian,并且我的测试类路径已添加到 ShrinkWrap 中。
My webapp has a non-empty production beans.xml
under src/main/resources/META-INF
.
Now, for my tests, I need to swap out 1 bean with an alternative.
Where do I put this test beans.xml
which contains just this and nothing more?
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<alternatives>
<class>...MyTestReplacement</class>
</alternatives>
</beans>
I tried under src/test/resources/META-INF
but that is ignored. I am using arquillian and my test classpath is added to the ShrinkWrap.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
即使它已经被接受,我也会向您发布我找到的解决方案。我遇到了同样的问题,使用 @Specializes 对我来说是没有选择的,因为我有很多带有多种方法的模拟,因此为每个方法创建一个类有点矫枉过正......
所以在我的测试中我有一个 ResourceMock
:下面的 ShrinkWrap 我只能在测试期间加载这些 @Alternative bean:(测试目录中不需要 beans.xml!)
就是这样。
Even though it's already accepted, ill post you the solution I found. I had the same problem, and using @Specializes was no option for me, because I had many mocks with several methods, thus create for each one a class was a bit overkill....
So in my test I have a ResourceMock:
With the following ShrinkWrap I was able to load those @Alternative bean only during the test: (no beans.xml in the test dir needed!)
And that's it.
不要使用@Alternative,而使用@Specializes。只需将 @Specializes bean 仅放入您的测试类路径中,它就会自动替换真正的 bean。无需搞乱 beans.xml。
Don't use @Alternative, but use @Specializes. Just put the @Specializes bean only in your test classpath and it will automatically replace the real bean. No need to mess around with beans.xml.
或者,如果您需要使用@Alternative,则可以使用收缩包装命令
.alternativeClass
。您可以在 Arquillian-showcase、cdi-ejb 子项目中看到以下示例。在您的 pom 中包含此依赖项:然后使用这种类型的 @Deployment(对于 Glassfish):
希望有帮助!
Or, if you need to use @Alternative, you can use a shrinkwrap command
.alternativeClass
. You can see the following example in the Arquillian-showcase, cdi-ejb subproject. Include this dependency in your pom:Then use this type of @Deployment (for Glassfish):
Hope that helps!
请参阅 http://seamframework.org/Seam3/ModuleTesting 或 http://ocpsoft.com/seam/cdi-powered-unit-testing-using-arquillian/
See http://seamframework.org/Seam3/ModuleTesting or http://ocpsoft.com/seam/cdi-powered-unit-testing-using-arquillian/
就我而言,我使用下面的代码来指定要包含哪个
beans.xml
文件:In my case I used the code from bellow to specify which
beans.xml
file to include: