Weld (CDI):我应该将配置的仅测试 beans.xml 放在哪里?

发布于 2024-12-03 17:22:27 字数 481 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

即使它已经被接受,我也会向您发布我找到的解决方案。我遇到了同样的问题,使用 @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");

就是这样。

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:

@Produces
@Alternative
public IResource createResource() {
    Resource resource = mock(Resource.class);
    when(resource.getLocalized(anyString())).then(AdditionalAnswers.returnsFirstArg());
    return resource;
}

With the following ShrinkWrap I was able to load those @Alternative bean only during the test: (no beans.xml in the test dir needed!)

return ShrinkWrap
            .create(JavaArchive.class)
            .addPackages(true, "some.package.with.classes")
            .addAsManifestResource(
                    new StringAsset(
                            "<alternatives><class>my.testclass.with.alternatives</class></alternatives>"),
                    "beans.xml");

And that's it.

舟遥客 2024-12-10 17:22:27

不要使用@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.

中二柚 2024-12-10 17:22:27

或者,如果您需要使用@Alternative,则可以使用收缩包装命令.alternativeClass。您可以在 Arquillian-showcase、cdi-ejb 子项目中看到以下示例。在您的 pom 中包含此依赖项:

    <dependency>
        <groupId>org.jboss.shrinkwrap.descriptors</groupId>
        <artifactId>shrinkwrap-descriptors-impl</artifactId>
        <scope>test</scope>
    </dependency>

然后使用这种类型的 @Deployment(对于 Glassfish):

    @Deployment
    public static WebArchive createDeploymentForGlassFish() {
    BeansDescriptor beansXml = Descriptors.create(BeansDescriptor.class);

    return ShrinkWrap.create(WebArchive.class)
            .addClasses(FireAndForget.class, FireAndForgetBean.class, BlockingFireAndForgetAlternativeBean.class)
            .addAsWebInfResource(
                    new StringAsset(beansXml.alternativeClass(BlockingFireAndForgetAlternativeBean.class).exportAsString()),
                    beansXml.getDescriptorName());
}

希望有帮助!

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:

    <dependency>
        <groupId>org.jboss.shrinkwrap.descriptors</groupId>
        <artifactId>shrinkwrap-descriptors-impl</artifactId>
        <scope>test</scope>
    </dependency>

Then use this type of @Deployment (for Glassfish):

    @Deployment
    public static WebArchive createDeploymentForGlassFish() {
    BeansDescriptor beansXml = Descriptors.create(BeansDescriptor.class);

    return ShrinkWrap.create(WebArchive.class)
            .addClasses(FireAndForget.class, FireAndForgetBean.class, BlockingFireAndForgetAlternativeBean.class)
            .addAsWebInfResource(
                    new StringAsset(beansXml.alternativeClass(BlockingFireAndForgetAlternativeBean.class).exportAsString()),
                    beansXml.getDescriptorName());
}

Hope that helps!

七月上 2024-12-10 17:22:27

就我而言,我使用下面的代码来指定要包含哪个 beans.xml 文件:

JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
                .addClasses(AutoService.class, AutoServiceCallerImp1.class, AutoServiceCallerImp2.class)
                .addAsManifestResource("META-INF/test.beans.xml", "beans.xml");

In my case I used the code from bellow to specify which beans.xml file to include:

JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
                .addClasses(AutoService.class, AutoServiceCallerImp1.class, AutoServiceCallerImp2.class)
                .addAsManifestResource("META-INF/test.beans.xml", "beans.xml");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文