【问题标题】:"No tests to run" with maven test goal具有 Maven 测试目标的“没有要运行的测试”
【发布时间】:2019-09-04 19:21:25
【问题描述】:

我使用的是 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 似乎有问题

我在包中的一个测试类位于&lt;projectDir&gt;/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()));
    }

}

【问题讨论】:

  • 省略spring-test 框架是否有效?
  • @Sormuras - nop
  • 你能确定一下2.22.1吗?根据github.com/junit-team/junit5-samples/blob/r5.5.0-M1/…,它确实适用于木星5.5.0-M1
  • 没有工作,即使使用 junit-jupiter 工件而不是引擎
  • 请出示您的测试课程?

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


【解决方案1】:

Maven 更倾向于约定而不是配置,并且具有标准的目录布局。 默认情况下,它需要在 src/test/java 中进行测试。

检查Introduction to the Standard Directory Layout。默认值在the super POM 中定义。

可以覆盖默认值(如果您有充分的理由这样做)。

【讨论】:

  • 如上所述,请仔细检查目录。 test/ 目录必须在src/ 下,NOTsrc/main/ 下:) 所以:src/main/java/com/example/thing/Hello.java 将在路径中进行测试:src/test/java/com/example/thing/HelloTest.java
猜你喜欢
  • 1970-01-01
  • 2020-12-19
  • 2018-07-08
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 2014-02-24
相关资源
最近更新 更多