14

I have a Spring Boot project. I have written unit tests in the .../src/test/java/... directories. All of my unit tests are of the form *Test.java. When I run 'mvn test', I get the following output:

[INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ frm-api --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 6 source files to /Users/JoelRives/Projects/fasor/fasor-service/fatality-review/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ frm-api --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] [INFO] Results: [INFO] [INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 [INFO] 

Clearly, maven sees the unit tests as it is compiling them. However, as you can see, it does not run them.

I have the following relevant maven dependencies in my pom:

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <version>1.5.3.RELEASE</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> <version>1.4.194</version> </dependency> 

My pom also includes the following plugin:

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.3.0-M1</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.2.0-M1</version> </dependency> </dependencies> </plugin> 

Any help regarding this is greatly appreciated.

I did not show my entire POM. Here is the Junit stuff:

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <version>1.5.3.RELEASE</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> <version>1.4.194</version> </dependency> 
3
  • Does your Test classes have a JUnitRunner ? Like '@RunWith(MyTestRunner.class)' Commented Jul 10, 2018 at 21:35
  • 1
    You are using Unit 4 but have configured surefire for junit5... Commented Jul 11, 2018 at 5:28
  • What part of my surefire plugin configures it for junit5? Commented Jul 11, 2018 at 14:58

6 Answers 6

14

It looks like if you are using Junit4 and the surefire plugin version 2.22 and above, the tests dont get picked up. I had a similar issue and using surefire V2.21.0 seemed to work.

Below is my surefire config

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.21.0</version> </plugin> 
Sign up to request clarification or add additional context in comments.

1 Comment

This turned out to be mine issue as well. Does that mean we can not use use higher versions of maven-surefire-plugin with Junit4 ?
13

For me, I had written this in my pom.xml and I was using JUnit4

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> 

Once I removed the exclusions, it started executing.

2 Comments

I had the same situation. Once I removed the excluded part it picked up all the tests.
Thanks @Ganesh for the above solution worked for me. I was using Junit 4 and even after i added surefire plugin, tests weren't being picked up. Removing the above exclusions did the trick!
4

Using recent surefire you can configure it to use JUnit 4:

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>3.0.0-M5</version> </dependency> </dependencies> </plugin> 

Comments

1

Note, this is a partial answer. I as able to get the unit tests to run by removing the surefire plugin from the POM. I have never had problems with surefire in the past. O well..., at least the tests are running -- broken as they may be :-) I will investigate the surefire issue later. Thanks for the suggestions.

Comments

1

Try updating the surefire-plugin to pick the test classes explicitly as:

<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <includes> <include>**/*Test*.java</include> </includes> </configuration> </plugin> </plugins> 

You can look into this as well, may be duplicated!!

2 Comments

exactly, I wonder will the code compile if there is JUnit dependency itself is missing and it is being used?
It helped in my case. By default, the plugin takes all the classes with the following name format: *Test.java, but skips all the tests that have anything in the name after the Test (e.g. IntegrationTest_4XX.java).
1

The key here is to understand the test engines that Spring uses.

In my case, I coded API tests with @RunWith(SpringRunner.class), which is under JUnit4, which runs with junit-vintage-engine.

But the unit tests are coded with JUnit Jupiter, which is under JUnit5, which runs with junit-jupiter-engine.

The default engine of SpringBoot is the junit-jupiter-engine. So we have to tell to Spring that we also want to use junit-vintage-engine.

We can simply add the dependency in our pom.xml:

<dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <scope>test</scope> </dependency> 

Or, if we want to use maven-surefire, we can add the dependency inside the plugin:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M7</version> <dependencies> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>5.7.0</version> </dependency> </dependencies> </plugin> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.