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:
- create a maven project with one unit test
SampleTestand the following pom. - run
mvn test. - see that
SampleTesthas run and failed - uncomment the block in
<dependencyManagement>to importSpring Boot - run
mvn test - see no tests have run and the build is succesfull.
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>