1

In a spring boot app, I have a test class as such

class UserInfoControllerTest {

    @Mock
    private UserService userService;
    @Mock
    private Principal principal;
    private UserInfoController subject;

    @BeforeEach
    void setUp() {
        //before each code
    }

    @Test
    void shouldReturnXYZ() {
       //test code
    }

    //more tests
}

The tests in these class are picked up by IntelliJ Idea, when for example, choosing the Run -> Run with coverage option.

But when I run mvn test or mvn clean test, none of these tests are picked up. How do I solve this issue?

Edit: Here is pom.xml

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

<groupId>com.example</groupId>
<artifactId>foo</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>

<name>Foo</name>
<description>Bar</description>

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

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>

<dependencies>

    <!-- spring boot and dependencies-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>



    <!-- For testing -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>RELEASE</version>
    </dependency>

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

I removed dependencies that are not related to testing, such as lombok, gson etc from the above file to keep the question small.

Registered User
  • 2,239
  • 3
  • 32
  • 58

1 Answers1

2

This is an "issue" with using Junit5 and Surefire you need to specify the the provider for Surefire to be able to run the Junit5 tests,

<plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.0</version>
                    </dependency>
                </dependencies>
</plugin>

It works within IntelliJ as IntelliJ has native support for Junit5 currently, same with Eclipse.

Darren Forsythe
  • 10,712
  • 4
  • 43
  • 54
  • Thanks, this helped. But it's not exactly correct. I found another question of which this is duplicate, although I'd have never found that if not for this answer. I'll close this as duplicate so that anyone who googles for this also finds the other one. – Registered User Feb 18 '18 at 14:43
  • 5
    It is also worth mentioning that you should import `org.junit.jupiter.api.Test` instead of ` org.junit.Test` – Jónás Balázs Jan 17 '20 at 10:42