【问题标题】:SpringBoot without parent pom failsafe integration tests are failing没有父 pom 故障安全集成测试的 Spring Boot 失败
【发布时间】:2018-09-25 12:17:35
【问题描述】:

我需要从我组织的父 pom 继承,所以我有以下设置。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-boot.version>2.0.5.RELEASE</spring-boot.version>
    <maven-failsafe-plugin.version>2.19.1</maven-failsafe-plugin.version>
    <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>build-info</goal>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${maven-failsafe-plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我的主要入口点类在 com.example.demo 包中。

现在我在com.example.demo 包中进行了如下集成测试:

package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationIT {

    @Test
    public void contextLoads() {
    }

}

当我运行 mvn clean install 时,集成测试失败并出现以下错误:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.526 sec <<< FAILURE! - in com.example.demo.DemoApplicationIT
initializationError(com.example.demo.DemoApplicationIT)  Time elapsed: 0.008 sec  <<< ERROR!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

如果我从spring-boot-maven-plugin 中删除&lt;goal&gt;repackage&lt;/goal&gt;,那么它工作正常。但我需要运行repackage 目标来构建胖罐。

有没有办法解决这个问题?

【问题讨论】:

    标签: java maven spring-boot maven-failsafe-plugin


    【解决方案1】:

    显然我遇到了这个问题https://github.com/spring-projects/spring-boot/issues/6254

    通过添加&lt;classesDirectory&gt;配置解决了这个问题,如下:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
    
        <configuration>
              <classesDirectory>${project.build.directory}/${artifactId}.jar.original</classesDirectory>
        </configuration>
    </plugin>
    

    【讨论】:

    • 这对我有用,但我花了更长的时间才意识到我的 jar 实际上被称为 ${artifactId}-${version}.jar.original
    • 已经看了好几个小时了。对 outputDirectory 的更改不起作用。终于找到了这个并解决了这个问题。谢谢!
    【解决方案2】:

    如果您不将 spring-boot-starter-parent 作为父级包含在您的 pom.xml 中,您将失去(除其他外)以下内容:

            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-failsafe-plugin</artifactId>
              <executions>
                <execution>
                  <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                  </goals>
                </execution>
              </executions>
              <configuration>
                <classesDirectory>${project.build.outputDirectory}</classesDirectory>
              </configuration>
            </plugin>
    

    您可以通过实际查看spring-boot-starter-parent 的 POM 文件来检查这一点,例如here.

    因此,在您的pom.xml 中,您需要将缺少的&lt;configuration&gt; 部分添加到您的maven-failsafe-plugin 定义中。

    【讨论】:

    • 你为我节省了一天的时间。谢谢!
    • 这是我见过的最常见的答案,但它不适用于 Spring Boot 2.5.6。我不得不使用 K. Siva 的回答 (stackoverflow.com/a/52502068/631272)。
    【解决方案3】:

    实际上,如果您创建自己的父级并计划在没有 spring-boot 插件的库中使用它将会失败。
    改为添加类目录
    http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#additionalClasspathElements
    ${project.build.directory}/classes

    【讨论】:

    • 我试过这个,它解决了来自 OP 的错误,但我仍然有一些其他问题,当我使用 @K 时没有发生这些问题。湿婆的回答。看起来有些依赖项没有解决,但我没有深入研究它,因为另一个答案工作正常。 YMMV!
    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 2019-12-26
    • 1970-01-01
    • 2017-07-02
    • 2021-05-18
    • 2020-03-16
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多