Maven测试目标“无需运行任何测试”

时间:2019-04-14 18:18:51

标签: maven unit-testing junit maven-surefire-plugin junit5

我正在使用Maven 3.5.2版,并且似乎具有正确的依赖性:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.devtop</groupId>
    <artifactId>discount-calculator</artifactId>
    <version>0.1</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <!--Testing dependencies-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.5.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.5.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.27.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.2.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>2.27.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
            </plugin>
        </plugins>
    </build>
</project>

但是,当我运行mvn clean test时,它找不到任何测试,只是打印出来:

[INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ discount-calculator ---
[INFO] No tests to run.

我的测试类确实以单词“ Test”结尾,并带有正确的注释,我可以通过intellij运行它们,但是maven似乎有问题

我在<projectDir>/src/main/test包中提供的测试类之一:

package com.devtop.discountcalculator.discount;

import com.devtop.discountcalculator.RuleReturnsTrue;
import org.junit.jupiter.api.Test;

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

public class RuleChainFactoryTest {

    @Test
    public void testChainRules_oneRule_exception() {
        assertThrows(IllegalArgumentException.class,
                () -> RuleChainFactory.getInstance().chainRules(new RuleReturnsTrue()));
    }

}

1 个答案:

答案 0 :(得分:1)

Maven favors convention over configuration, and has standard directory layout. By default, it expects tests in src/test/java.

Check Introduction to the Standard Directory Layout. The defaults are defined in the super POM.

The defaults can be overriden (if you have good reason to do so).