Maven不运行单元测试-方法名称约定

时间:2019-07-22 20:02:53

标签: java maven junit5 maven-surefire-plugin

我想与大家分享我今天尝试对多模块项目进行Maven单元测试时遇到的奇怪经验。

当我直接从IDE(IntelliJ)运行测试时,一切正常。但是对于Maven,只执行一个测试类。我花了几个小时试图找出这种行为的原因。事实证明,仅执行名称以testxxx () 开头的方法。

我非常惊讶,因为我知道类级别的命名约定,但是不知道方法级别的命名约定。

您能理解会发生什么吗?

这里是我正在使用的堆栈:Maven 3.6.0,JUnit 5,maven-surefire-plugin:3.0.0

编辑:此处是父级POM的依赖项部分:

 <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>

这是一个类测试的示例,由于测试方法不是以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");

    }
}

1 个答案:

答案 0 :(得分:0)

include部分添加到surefire插件配置中。 示例:

                <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>