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

beq87vna  于 2023-05-06  发布在  Maven
关注(0)|答案(2)|浏览(144)

我想和大家分享一个奇怪的经历,我今天尝试用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");

    }
}
sqxo8psd

sqxo8psd1#

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

tf7tbtn22#

我也有同样的问题。我解决了这个问题,将surefire更新到了新版本(目前是2.22.2)

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

相关问题