【问题标题】:@MicronautTest does not start the embedded server@MicronautTest 不启动嵌入式服务器
【发布时间】:2019-04-09 08:09:46
【问题描述】:

我正在使用 Micronaut 为应用程序中的控制器编写 Spock 测试。使用@MicronautTest(application=Application)时,会抛出异常消息@MicronautTest used on test but no bean definition for the test present..

在检查代码时,我发现 Micronaut 抛出此异常的原因有以下 2 个。来自io.micronaut.test.extensions.spock.MicronautSpockExtension

    if (this.specDefinition == null) {
        if (!this.isTestSuiteBeanPresent((Class)spec.getReflection())) {
          throw new InvalidSpecException("@MicronautTest used on test but no bean definition for the test present. This error indicates a misconfigured build or IDE. Please add the 'micronaut-inject-java' annotation processor to your test processor path (for Java this is the testAnnotationProcessor scope, for Kotlin kaptTest and for Groovy testCompile). See the documentation for reference: https://micronaut-projects.github.io/micronaut-test/latest/guide/");
        }
    ...
    }

我的 POM 配置是:

     <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.6</version>
        <executions>
          <execution>
            <goals>
              <goal>addTestSources</goal>
              <goal>addSources</goal>
              <goal>compileTests</goal>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>${jdk.version}</source>
          <target>${jdk.version}</target>
          <encoding>UTF-8</encoding>
          <compilerArgs>
            <arg>-parameters</arg>
          </compilerArgs>
          <annotationProcessorPaths>
            <path>
              <groupId>org.mapstruct</groupId>
              <artifactId>mapstruct-processor</artifactId>
              <version>${mapstruct.version}</version>
            </path>
            <path>
              <groupId>io.micronaut</groupId>
              <artifactId>micronaut-inject-java</artifactId>
              <version>${micronaut.version}</version>
            </path>
            <path>
              <groupId>io.micronaut</groupId>
              <artifactId>micronaut-validation</artifactId>
              <version>${micronaut.version}</version>
            </path>
          </annotationProcessorPaths>
        </configuration>
        <executions>
          <execution>
            <id>test-compile</id>
            <goals>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <compilerArgs>
                <arg>-parameters</arg>
              </compilerArgs>
              <annotationProcessorPaths>
                <path>
                  <groupId>org.mapstruct</groupId>
                  <artifactId>mapstruct-processor</artifactId>
                  <version>${mapstruct.version}</version>
                </path>
                <path>
                  <groupId>io.micronaut</groupId>
                  <artifactId>micronaut-inject-java</artifactId>
                  <version>${micronaut.version}</version>
                </path>
              </annotationProcessorPaths>
            </configuration>
          </execution>
        </executions>
      </plugin>

如果我没有定义测试@MicronautTest 的注释,似乎应用程序甚至没有启动。

以下是规范代码:

@MicronautTest(application= Application)
@PropertySource(value = [
        @Property(name='spec.name', value = 'EndpointSpec'),
        @Property(name = 'endpoints.health.details-visible', value = 'ANONYMOUS'),
        @Property(name = MongoSettings.EMBEDDED, value = 'true'),
])
class EndpointSpec extends Specification {

    @Inject
    EmbeddedServer embeddedServer
    @Inject
    ApplicationContext applicationContext

    @Unroll
    def "test health endpoint is working"() {
        given: 'a RxHttpClient'
        URL server = embeddedServer.getURL()
        RxHttpClient client = new DefaultHttpClient(server, new DefaultHttpClientConfiguration(), '/management')

        when: '/health is called'
        HttpResponse response = client.toBlocking().exchange('/health')

        then: 'response is 200 OK and contains valid headers'
        response.status == HttpStatus.OK
        response.headers.size() == 5
        response.headers.contains('uber-trace-id')
        response.headers.contains('Date')
        response.headers.contains('content-type') && response.headers.get('content-type') == MediaType.APPLICATION_JSON
        response.headers.contains('content-length')
        response.headers.contains('connection')

        //and: 'response contains valid HealthResult'
        //HealthResult healthResult = response.body()
        // Want to validate the health result here but nothing is present in body

    }
}

我如何定义specDefinition 值或标记测试以使其作为bean 定义存在,以及这种行为的原因是什么。任何帮助将不胜感激。

【问题讨论】:

  • 请发布您的规范代码,否则很难诊断您的问题。
  • 对不起。请在上面找到它。有趣的事实:我将我的项目转换为 Gradle,我开始收到 HTTP 响应代码 200 但没有正文。如果我在端点上放置一个调试器,我会看到一个适当的主体作为响应。
  • 这里更新:我从当前的HttpResponse response = client.toBlocking().exchange('/health') 更改了客户端检索的代码 -> HttpResponse response = client.exchange('/health').blockingSingle() 并且这有效。我可以得到响应正文。为什么这与任何想法不同?

标签: testing spock micronaut


【解决方案1】:

Micronaut-test 使测试本身成为 bean。为了让 Groovy 测试成为 bean,您需要在 test 的编译路径上包含 micronaut-inject-groovy

【讨论】:

  • 谢谢@james。正如我之前提到的,在使用 Gradle 构建时,它可以工作。问题仅在使用 Maven 时出现。我确定我错过了任何依赖,但无法弄清楚。还有client.toBlocking().exchange(uri)client.exchange(uri).blockingSingle()的区别
  • 实际上不应该有任何区别
【解决方案2】:

是的,将 micronaut-inject-groovy 添加到您的 maven 构建将解决该问题。添加以下内容。

<dependency>
   <groupId>io.micronaut.test</groupId>
   <artifactId>micronaut-test-spock</artifactId>
   <scope>test</scope>
</dependency>

【讨论】:

    猜你喜欢
    • 2020-05-18
    • 2020-10-14
    • 2019-10-11
    • 1970-01-01
    • 2018-07-12
    • 2010-11-04
    • 2020-12-19
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多