没有父pom故障安全集成测试的SpringBoot失败

时间:2018-09-25 12:17:36

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

我需要从组织的上级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

如果我从<goal>repackage</goal>中删除了spring-boot-maven-plugin,那么它工作正常。但是我需要运行repackage目标来建立胖子。

反正有解决此问题的方法吗?

3 个答案:

答案 0 :(得分:0)

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

通过添加<classesDirectory>配置来解决此问题,如下所示:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>

    <configuration>
          <classesDirectory>${project.build.directory}/${artifactId}.jar.original</classesDirectory>
    </configuration>
</plugin>

答案 1 :(得分:0)

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

答案 2 :(得分:0)

如果您未将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中,您需要将缺少的<configuration>部分添加到maven-failsafe-plugin定义中。