@QuarkusTest 的 bean 发现可配置吗?

发布于 2025-01-18 16:08:36 字数 2574 浏览 3 评论 0原文

考虑以下Quarkus应用程序:

@ApplicationScoped
@Path("/hello")
public class HelloService {

    @Inject
    Helper helper;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello(){
        return "Hello "+helper.getName();
    }
}


@Dependent
public class Helper {

    public String getName() {
        return "World";
    }
}

以及以下测试:

@QuarkusTest
@TestHTTPEndpoint(HelloService.class)
public class HelloQuarkusTest {

    @Test
    void testSayHello(){
        given().when().then().body(is("Hello World"));
    }
}

到目前为止一切都很好。当然,该服务及其助手比实际上要复杂得多。所以我想进行一些单位测试。如果我这样的服务编写单元测试:

@EnableAutoWeld
@ExtendWith(MockitoExtension.class)
class HelloUnitTest {

    @Inject
    HelloService helloService;

    @Produces @Mock
    @ExcludeBean // don't use the real bean, use this instead
    Helper helperMock;

    @Test
    void testSayHello(){
        when(helperMock.getName()).thenReturn("Mock");
        final String actual = helloService.sayHello();
        assertThat(actual).isEqualTo("Hello Mock");
    }
}

那么单位测试是绿色的,但是突然@quarkustest失败了,因为它同时找到了真实的helper实现和从单位测试中的生产者字段,因此抛出了模棱两可的Exception

java.lang.RuntimeException: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
    [error]: Build step io.quarkus.arc.deployment.ArcProcessor#validate threw an exception: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.AmbiguousResolutionException: Ambiguous dependencies for type com.example.Helper and qualifiers [@Default]
    - java member: com.example.HelloService#helper
    - declared on CLASS bean [types=[com.example.HelloService, java.lang.Object], qualifiers=[@Default, @Any], target=com.example.HelloService]
    - available beans:
        - CLASS bean [types=[java.lang.Object, com.example.Helper], qualifiers=[@Default, @Any], target=com.example.Helper]
        - PRODUCER FIELD bean [types=[java.lang.Object, com.example.Helper], qualifiers=[@Default, @Any], target=com.example.Helper com.example.weld.HelloUnitTest.helperMock, declaringBean=com.example.weld.HelloUnitTest]

即使测试在不同的软件包中,也会发生这种情况。在某些方面,我明白为什么扫描多个类/软件包的目的是@quarkustest,但在这种情况下我不喜欢它。

所以我的问题是:

如何配置什么bean/class/packages a @quarkustest拾取并忽略哪个?

我尝试在application-test.properties中使用ARC设置进行播放,但到目前为止尚未找到正确的属性组合。

Consider the following Quarkus application:

@ApplicationScoped
@Path("/hello")
public class HelloService {

    @Inject
    Helper helper;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello(){
        return "Hello "+helper.getName();
    }
}


@Dependent
public class Helper {

    public String getName() {
        return "World";
    }
}

and the following Test:

@QuarkusTest
@TestHTTPEndpoint(HelloService.class)
public class HelloQuarkusTest {

    @Test
    void testSayHello(){
        given().when().then().body(is("Hello World"));
    }
}

Everything's fine up to this point. The service and its helper are of course a bit more complicated than that in reality. So I want to have some unit tests. If I write a unit test for the service like so:

@EnableAutoWeld
@ExtendWith(MockitoExtension.class)
class HelloUnitTest {

    @Inject
    HelloService helloService;

    @Produces @Mock
    @ExcludeBean // don't use the real bean, use this instead
    Helper helperMock;

    @Test
    void testSayHello(){
        when(helperMock.getName()).thenReturn("Mock");
        final String actual = helloService.sayHello();
        assertThat(actual).isEqualTo("Hello Mock");
    }
}

Then the unit test is green, but suddenly the @QuarkusTest fails, because it finds both the real Helper implementation and the producer field from the unit test and consequently throws an AmbiguousResolutionException:

java.lang.RuntimeException: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
    [error]: Build step io.quarkus.arc.deployment.ArcProcessor#validate threw an exception: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.AmbiguousResolutionException: Ambiguous dependencies for type com.example.Helper and qualifiers [@Default]
    - java member: com.example.HelloService#helper
    - declared on CLASS bean [types=[com.example.HelloService, java.lang.Object], qualifiers=[@Default, @Any], target=com.example.HelloService]
    - available beans:
        - CLASS bean [types=[java.lang.Object, com.example.Helper], qualifiers=[@Default, @Any], target=com.example.Helper]
        - PRODUCER FIELD bean [types=[java.lang.Object, com.example.Helper], qualifiers=[@Default, @Any], target=com.example.Helper com.example.weld.HelloUnitTest.helperMock, declaringBean=com.example.weld.HelloUnitTest]

This happens even if the tests are in different packages. To some extend I understand why scanning more than one class/package is intended for a @QuarkusTest, but I don't like it in this case.

So my question is:

How can I configure what beans/classes/packages a @QuarkusTest picks up and which it ignores?

I have tried playing around with the Arc settings in application-test.properties, but so far have not found the right combination of properties.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

草莓味的萝莉 2025-01-25 16:08:36

您可以尝试在特定软件包中添加所有焊接点单元测试,并通过 quarkus.arc.Arc.exclude-types ;即类似%test.quarkus.arc.arc.exclude-types = org.acme.unit.tests。**之类的东西。

You could try to add all the weld-junit unit tests in a specific package and exclude this package from quarkus discovery via quarkus.arc.exclude-types; i.e. something like %test.quarkus.arc.exclude-types=org.acme.unit.tests.**.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文