【问题标题】:How to get Quarkus multimodule CDI working in tests?如何让 Quarkus 多模块 CDI 在测试中工作?
【发布时间】:2021-01-16 09:40:17
【问题描述】:

在 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,但在测试上下文中没有?为什么这个问题特别出现在多模块设置中?更重要的是,如何解决这个问题?

【问题讨论】:

    标签: testing cdi quarkus


    【解决方案1】:

    我正在使用(当时)最新版本的 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>
    

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 2021-07-05
      • 1970-01-01
      • 2021-12-18
      相关资源
      最近更新 更多