我的 webapp 在 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。
最佳答案
即使它已经被接受,我还是把我找到的解决方案发给你。我遇到了同样的问题,使用@Specializes 对我来说是没有选择的,因为我有很多带有几种方法的模拟,因此为每个模拟创建一个类有点矫枉过正......
所以在我的测试中我有一个 ResourceMock:
@Produces
@Alternative
public IResource createResource() {
Resource resource = mock(Resource.class);
when(resource.getLocalized(anyString())).then(AdditionalAnswers.returnsFirstArg());
return resource;
}
使用以下 ShrinkWrap,我只能在测试期间加载那些 @Alternative bean:(不需要测试目录中的 beans.xml!)
return ShrinkWrap
.create(JavaArchive.class)
.addPackages(true, "some.package.with.classes")
.addAsManifestResource(
new StringAsset(
"<alternatives><class>my.testclass.with.alternatives</class></alternatives>"),
"beans.xml");
就是这样。
关于jboss-weld - 焊接 (CDI) : where do I put my test-only beans. 配置 <alternatives> 的 xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7349045/