Julien Berthoud
Julien Berthoud

Reputation: 769

Maven doesn't run unit tests - method name convention

I'd like to share with you a weird experience I had today when I was trying to execute with maven unit tests for a multi-modules project.

When I ran the tests directly from the IDE (IntelliJ), everything works perfectly. But with Maven only one test class was being executed. I spend hours trying to figure out the reason for this behaviour. It turns out that only the methods whose name start with testxxx () are executed.

I'm very suprised because I knew about the naming convention at class level, but was not aware about a naming convention at method level.

Can you understand what happens?

Here the stack I'm working with: Maven 3.6.0, JUnit 5, maven-surefire-plugin:3.0.0

Edit: here the dependency section of the POM of the parent level :

 <dependencyManagement>
        <dependencies>
            <!-- ========== Modules ========== -->
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>myerp-technical</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>myerp-model</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>myerp-consumer</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>myerp-business</artifactId>
                <version>${project.version}</version>
            </dependency>


            <!-- ========== Libraries ========== -->
            <!-- ===== Log4j ===== -->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <!-- Commons Logging Bridge -->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-jcl</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <!-- Log4j 2 SLF4J Binding -->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j-impl</artifactId>
                <version>${log4j.version}</version>
            </dependency>

            <!-- ===== JSR 303 - Bean validation ===== -->
            <!-- interface -->
            <dependency>
                <groupId>javax.validation</groupId>
                <artifactId>validation-api</artifactId>
                <version>1.1.0.Final</version>
            </dependency>
            <!--implementation-->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>4.2.0.Final</version>
            </dependency>

            <!-- ===== Apache Commons ===== -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-collections4</artifactId>
                <version>4.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.5</version>
            </dependency>

            <!-- ===== Spring IOC ===== -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
                <scope>compile</scope>
            </dependency>

            <!-- ===== Spring JDBC/Tx ===== -->
            <!-- spring-tx : transaction, JCA, DAO -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
                <scope>compile</scope>
            </dependency>
            <!-- spring-jdbc : commons-exceptions, datasource management -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
                <scope>compile</scope>
            </dependency>

            <!-- ===== Database ===== -->
            <!-- DB Connection pool -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-dbcp2</artifactId>
                <version>2.1.1</version>
            </dependency>
            <!-- JDBC Drivers : PostgreSQL -->
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>9.4.1212</version>
                <scope>runtime</scope>
            </dependency>

            <!-- ===== unit tests===== -->

            <dependency>
                <groupId>com.github.sbrannen</groupId>
                <artifactId>spring-test-junit5</artifactId>
                <version>1.4.0</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-junit-jupiter</artifactId>
                <version>3.0.0</version>
                <scope>test</scope>
            </dependency>


        </dependencies>
    </dependencyManagement>

And here an example of a class test, which is being ignored because the test method doesn't start with testXXXX():

package com.dummy.myerp.model.bean.comptabilite;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestDummy {


    @Test
    public void getByNumeroTest() {

        Integer numero1 = 1232;
        Integer numero2 = 92837;
        Integer numero3 = null;

        String libellé1 = "apports en capital";
        String libellé2 = null;
        String libellé3 = "produits exceptionnels";

        CompteComptable compteComptable1 = new CompteComptable(numero1, libellé1);
        CompteComptable compteComptable2 = new CompteComptable(numero2, libellé2);
        CompteComptable compteComptable3 = new CompteComptable(numero3, libellé3);

        List<CompteComptable> list3elements = new ArrayList<CompteComptable>(
                Arrays.asList(compteComptable1, compteComptable2, compteComptable3));


        assertEquals(compteComptable1, CompteComptable.getByNumero(list3elements, numero1), "3 elements, standard");
        assertEquals(compteComptable2, CompteComptable.getByNumero(list3elements, numero2), "3 elements, name is null");

        assertEquals(null, CompteComptable.getByNumero(list3elements, numero3), "3 elements, account is null");
        assertEquals(null, CompteComptable.getByNumero(list3elements, 5555), "3 elements, account not available");

    }
}

Upvotes: 2

Views: 1251

Answers (2)

rasklaad
rasklaad

Reputation: 180

I had the same issue. I solved it with updating surefire to the newer version (2.22.2 at the moment)

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
    </plugin>

Upvotes: 0

Eduard Dubilyer
Eduard Dubilyer

Reputation: 1013

Add include section to the surefire plugin configuration. Example:

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M3</version>
                    <configuration>
                        <includes>*</includes>
                        <properties>
                            <configurationParameters>
                                junit.jupiter.extensions.autodetection.enabled = true
                            </configurationParameters>
                        </properties>
                    </configuration>
                </plugin>

Upvotes: 1

Related Questions