My project is supposed to be inherited from a custom parent and at the same time use Spring Boot
. The standard solution for that is using <dependencyManagement>
section as described here .
The problem is when a Spring Boot
dependency is added into the section, maven does not see any unit tests (Test runs = 0). I reproduced that with artifacts spring-boot-dependencies
and spring-boot-starter
.
Steps to reproduce:
SampleTest
and the following pom.mvn test
.SampleTest
has run and failed<dependencyManagement>
to import Spring Boot
mvn test
I had similar problems with older versions of surefire
, which could not find JUnit5
tests in a similar manner. But the effective pom shows that in this case plugins' versions are correctly set to 3.0.0-M4
and not overridden by SpringBoot.
Could you help me fix the problem with running tests and properly apply Spring Boot
in this case?
src/test/java/org/example/SampleTest.java
package org.example;
import org.junit.jupiter.api.*;
class SampleTest{
@Test
void test(){
Assertions.assertEquals(0, 1);
}
}
pom.xml
The following sample does not have a parent: I've just copied plugins and dependencies from that to keep it concise.
<?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>org.example</groupId>
<artifactId>spring-boot-import</artifactId>
<version>1.0</version>
<dependencyManagement>
<!-- when this block is uncommented no unit tests are found in the project-->
<!--
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
-->
</dependencyManagement>
<properties>
<version.java>11</version.java>
<version.junit.jupiter>5.6.0</version.junit.jupiter>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerVersion>${version.java}</compilerVersion>
<release>${version.java}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.junit.jupiter}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Including the spring-boot-dependencies
dependencies changes the transitive dependencies of the junit-jupiter-engine
dependency.
The output of mvn dependency:tree
will show this.
Before
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ spring-boot-import ---
[INFO] org.example:spring-boot-import:jar:1.0
[INFO] \- org.junit.jupiter:junit-jupiter-engine:jar:5.6.0:test
[INFO] +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO] +- org.junit.platform:junit-platform-engine:jar:1.6.0:test
[INFO] | +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] | \- org.junit.platform:junit-platform-commons:jar:1.6.0:test
[INFO] \- org.junit.jupiter:junit-jupiter-api:jar:5.6.0:test
After
[INFO] org.example:spring-boot-import:jar:1.0
[INFO] \- org.junit.jupiter:junit-jupiter-engine:jar:5.6.0:test
[INFO] +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO] +- org.junit.platform:junit-platform-engine:jar:1.5.2:test
[INFO] | +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] | \- org.junit.platform:junit-platform-commons:jar:1.5.2:test
[INFO] \- org.junit.jupiter:junit-jupiter-api:jar:5.5.2:test
As you can see the junit-platform-engine
and friends changed from 1.6.0 to 1.5.2 and the API from 5.6.0 to 5.5.2 . As those jars are incompatible versions your tests won't run anymore.
To fix you can do 1 of the following
junit-bom
as well to the dependencyManagement
section before the Spring Boot one, to force the versionsThe other solution is simply to define the import for junit jupiter before spring boot. That solves the issue also.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.6.0</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
if you are using Spring Boot 2.x
then by default it supports JUnit 5
and all the test cases written in JUnit 4
will not be read while building the project or while executing mvn
test. To fix it, use the below snippet in pom.xml
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<!-- Use the older JUnit 4 provider -->
<artifactId>surefire-junit4</artifactId>
<version>2.8</version>
</dependency>
</dependencies>
</plugin>
Note: using the above will skip your JUnit 5
test cases.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.