没有父 pom 故障安全集成测试的 Spring Boot 失败
SpringBoot without parent pom failsafe integration tests are failing
我需要从我组织的父 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>
我的主要入口点 class 在 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
中删除 <goal>repackage</goal>
那么它工作正常。但是我需要运行repackage
目标来构建fat jar。
有办法解决这个问题吗?
显然我遇到了这个问题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>
实际上,如果您创建自己的父级并计划在没有 spring-boot 插件的库中使用,它将失败。
添加 类 目录而不是
http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#additionalClasspathElements
${project.build.directory}/类
如果您不将 spring-boot-starter-parent
作为 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
定义中。
我需要从我组织的父 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>
我的主要入口点 class 在 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
中删除 <goal>repackage</goal>
那么它工作正常。但是我需要运行repackage
目标来构建fat jar。
有办法解决这个问题吗?
显然我遇到了这个问题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>
实际上,如果您创建自己的父级并计划在没有 spring-boot 插件的库中使用,它将失败。
添加 类 目录而不是
http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#additionalClasspathElements
${project.build.directory}/类
如果您不将 spring-boot-starter-parent
作为 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
定义中。