如何让 Quarkus 多模块 CDI 在测试中工作?
How to get Quarkus multimodule CDI working in tests?
在 Quarkus 测试中,可以通过使用 @Inject 注释来轻松使用 bean:
package com.test;
@QuarkusTest
class InjectionTest {
@Inject
SomeBean someBean;
@Test
void someTest() {
// Testing logic, assertions and all
}
“SomeBean”:
package com.test;
@ApplicationScoped
public class SomeBean {
}
不幸的是,这会在使用多模块设置时导致错误。可以在此处找到此类设置的示例项目:https://github.com/lssoares/multi-maven-quarkus
要重现,只需尝试将任何 bean 注入任何测试。
抛出的错误看起来像这样:
Build step io.quarkus.arc.deployment.ArcProcessor#validate threw an exception: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type com.test.SomeBean and qualifiers [@Default]
- java member: com.test.InjectionTest#someBean
当我 运行 应用程序时,这个 bean 的注入非常顺利。
为什么 Quarkus 在正常上下文中有 bean,但在测试上下文中没有?为什么这个问题特别发生在多模块设置中?更重要的是,如何解决这个问题?
我使用的是(当时)最新版本的 Quarkus:1.10.5.Final。升级到目前最新版本后:1.11.0.Final问题解决。
旧:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-universe-bom</artifactId>
<version>1.10.5.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>1.10.5.Final</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
新:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-universe-bom</artifactId>
<version>1.11.0.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>1.11.0.Final</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
在 Quarkus 测试中,可以通过使用 @Inject 注释来轻松使用 bean:
package com.test;
@QuarkusTest
class InjectionTest {
@Inject
SomeBean someBean;
@Test
void someTest() {
// Testing logic, assertions and all
}
“SomeBean”:
package com.test;
@ApplicationScoped
public class SomeBean {
}
不幸的是,这会在使用多模块设置时导致错误。可以在此处找到此类设置的示例项目:https://github.com/lssoares/multi-maven-quarkus 要重现,只需尝试将任何 bean 注入任何测试。
抛出的错误看起来像这样:
Build step io.quarkus.arc.deployment.ArcProcessor#validate threw an exception: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type com.test.SomeBean and qualifiers [@Default]
- java member: com.test.InjectionTest#someBean
当我 运行 应用程序时,这个 bean 的注入非常顺利。 为什么 Quarkus 在正常上下文中有 bean,但在测试上下文中没有?为什么这个问题特别发生在多模块设置中?更重要的是,如何解决这个问题?
我使用的是(当时)最新版本的 Quarkus:1.10.5.Final。升级到目前最新版本后:1.11.0.Final问题解决。
旧:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-universe-bom</artifactId>
<version>1.10.5.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>1.10.5.Final</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
新:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-universe-bom</artifactId>
<version>1.11.0.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>1.11.0.Final</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>