jboss-weld - 焊接 (CDI) : where do I put my test-only beans. 配置 <alternatives> 的 xml?

标签 jboss-weld jboss-arquillian

我的 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/

相关文章:

java - ArquillianCucumber 找不到功能文件

java - Arquillian 测试仅在放置在根包中时才有效

seam - 焊接原型(prototype) vs Seam Forge

java - Google Guice 与 JSR-299 CDI/Weld

java - Weld 中使用 @Inject 进行依赖注入(inject) (JSR-299 RI)。对应的@Produces是如何找到的?

testing - 对于 OSGi(集成)测试,Arquillian 与 Pax Exam 相比如何?

java - Graphite 烯抽象 - 断言的最佳实践

java - Arquillian 运行模式

java - CDI 扩展 - 在 ProcessAnnotatedType 阶段添加拦截器

java - 为什么我必须在此示例中为 Weld 添加私有(private)构造函数?