58

I have a rest Spring Boot REST API that I want to test. I can run the tests manually in Eclipse (without maven and by running the application as JUnit test) and it runs fine and displays the results, but mvn test does not "work" as you will find out below.

Here is my POM file:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>org.demo</groupId>
<artifactId>rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>UserRegistrationServices</name>
<description>RESTful API</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <!-- Junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- Spring dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <!-- To deploy to external servlet container -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- For Spring Boot testing -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>2.4.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>2.4.1</version>
        <scope>test</scope>
    </dependency>

    <!-- For returning objects as JSON -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.5.4</version><!--$NO-MVN-MAN-VER$ -->
    </dependency>
    <dependency>
        <groupId>com.fasterxml</groupId>
        <artifactId>jackson-xml-databind</artifactId>
        <version>0.6.2</version>
    </dependency>

    <!-- To decode Base64 data -->
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.10</version>
    </dependency>
</dependencies>

<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
    </pluginRepository>
</pluginRepositories>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

This is the result of mvn test:

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building UserRegistrationServices 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ rest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ rest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 9 source files to C:\Users\pmandayam\git\UserRegistrationServices\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ rest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\pmandayam\git\UserRegistrationServices\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ rest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\pmandayam\git\UserRegistrationServices\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ rest ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.768 s
[INFO] Finished at: 2015-07-28T12:07:41-05:00
[INFO] Final Memory: 24M/212M
[INFO] ------------------------------------------------------------------------

Here is a segment of my TestController.java class in src/test/java:

@Test
    public void f_findByUsername() {
        // Finding user with username 'user1username'

        given().auth().basic("User1username", "Testpassword").when().get(
                "http://localhost:8080/users/get/ByUsername?username=User1username")
                .then().assertThat().body("username", is("User1username"));
    }

At the top of the TestController class I have these annotations:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
/* Tells the embedded Tomcat server to start on a random, open port */
@IntegrationTest("server.port:0")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestController {....}

I'm not sure whats wrong. I don't have the surefire plugin but its looking for that it seems.

15 Answers 15

61

Use the below maven jar. It fixed my issue.

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
</dependency>
4
  • 14
    This is necessary if you're using JUnit 4 with the latest Spring Boot version, see: baeldung.com/spring-boot-testing#1-junit-4
    – AlexO
    Commented Apr 13, 2021 at 14:27
  • 4
    You saved my day. This dependency is required for the latest spring boot applications.
    – sampathlk
    Commented Oct 21, 2021 at 21:30
  • this sollutions is work if you are using org.junit.Test Commented Jan 3, 2022 at 10:43
  • Mamma Mia! Wonder why my test coverage went to 0... Commented Jun 20, 2023 at 20:16
56

The code in the class you named TestController isn't a controller, it's a test, but the convention says that it's a controller (perhaps used in testing). By default, Surefire will be looking for tests matching *Test; rename the class to ControllerTest.

2
  • Important statement about the naming scheme for test-driven programming, indeed!
    – B--rian
    Commented Aug 16, 2017 at 13:57
  • 1
    Works! I had same problem and after renaming to "...Test" Maven begins to execute my tests.
    – Ev.Rei.
    Commented Dec 7, 2017 at 15:38
27

try checking if you are using the correct package for @Test annotation. In Spring Boot 2.3.6 is org.junit.jupiter.api.Test

1
  • +1. Fixed by using this class in test method annotation:
    – Alex
    Commented Jul 20, 2022 at 17:17
17

Even if this is not recommended (as not standard), you can configure the maven surefire plugin too, as follows:

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

Edit: Wildcard added before /Test*.java

15

As this question populates first on search engines I thought it would be helpful for the people looking for the solution for the wired behavior of SpringBoot from version 2.4 onwards.

Problem - When you migrate your SpringBoot project from older version to newer like 2.5.x then the existing junit test cases are ignored by maven.

Reason - SpringBoot has been migrated to junit5 and thats why maven is ignoring junit4 test cases. To see detailed test dependencies explore the pom for spring-boot-starter-test.

Solution 1 - Either you write your test cases in junit5 with class level annotation @SpringBootTest and follow Junit5 rules.

Solution 2 - Or you can use junit-vintage dependency in your project to run both junit4 and junit5 test cases together.

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.6.3</version>
    <scope>test</scope>
</dependency>
0
6

Another reason this may be happening is to have declared another surefire plugin in your pom. In my case I migrated an app to spring boot and left this in the pom.

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <testFailureIgnore>false</testFailureIgnore>
                <includes>
                    <include>**/*Test*.java</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <phase>clean</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Spring boot test were executed after remove this part from the pom.

6

If none of the above works for you and you are using junit 5 with appropriate annotations, issue might be with Maven Surefire and Failsafe plugins. This is because of some conflict between the JUnit Surefire provider and the JUnit support in the Surefire 2.22.0 plugin release.
Update your POM to require the 2.22.0 releases of the Maven Surefire and Failsafe plugins.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
        </plugins>
    </build>

Refer here for an example

2

I will add one more new answer here, basically you could import the wrong @Test annotation:

Good one:

import org.junit.jupiter.api.Test;

Bad one:

import org.junit.Test;
1

This issue can happen because your test class is not public.

1

If you are facing this issue with spring boot 3, I had to upgrade my surefire plugin version to 3.0.0-M8

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M8</version>
</plugin>
0

With Spring boot 2.5.0 junit-vintage-engine jar isn't added, hence any org.junit.Test won't be run.

Only org.junit.jupiter.api.Test seems to run.

After Adding this jar (junit-vintage-engine) all my old tests(org.junit.Test) run absolutely fine.

0

Adding org.junit.jupiter.api.Test worked for me

2
0

this works for my problem to update springboot 2.3.9 to 2.4.3

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

TestController - file name should end with Test.java

1
-5

For other people who still have the issue. Personally I resolved that by removing :

<packaging>pom</packaging>

from the pom.xml file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.