【问题标题】:I can´t run my tests using mvn test in Maven我无法在 Maven 中使用 mvn test 运行我的测试
【发布时间】:2020-12-11 16:10:20
【问题描述】:

我有以下 POM:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     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.manning.junitbook</groupId>
<artifactId>ch13-continuous</artifactId>
<version>1.0-SNAPSHOT</version>

<name>ch13-continuous</name>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <sonar.coverage.jacoco.xmlReportPaths>target\site\jacoco\jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
    <sonar.junit.reportPaths>target\surefire-reports</sonar.junit.reportPaths>

</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.5</version>
            <executions>
                <execution>
                    <id>report</id>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <phase>verify</phase>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
        </plugin>
    </plugins>


</build>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.6.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.6.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>


</dependencies>

我的目录 /test/java/es.ull/flights 和 /test/java/es.ull/passenger 分别有一些名为“FlightTests”和“PassengerTests”的测试。但是,当我在 IntelliJ 上启动 mvn test 时,它最终显示如下:

[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

为什么会这样??我认为它无法本地化我的测试,但我不知道为什么。

【问题讨论】:

  • junit5 也有类似的问题,不能使用surefile 插件运行
  • 当你说/test/java是指src下的那个,即/src/test/java
  • 运行tree 或类似的命令并显示您的项目目录的文件和文件夹组织,并将其编辑到问题中

标签: java maven testing intellij-idea


【解决方案1】:

您有一些名为“FlightTests”和“PassengerTests”的测试文件,这就是问题所在,您能否通过删除末尾的 s 将名称更改为“FlightTest”和“PassengerTest”,否则它将无法识别测试。我已经验证过了。

【讨论】:

    【解决方案2】:

    也许你可以试试新版本。

       <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                  <includes>
                    <include>*Test.java</include>
                  </includes>
                </configuration>
      </plugin>
    

    您还可以使用 jupiter 并尝试使用 jupiter 依赖关系的 junit5。 例如使用来自 jupiter 依赖的 @Test 注解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 2020-01-12
      • 2019-02-19
      • 2021-09-08
      • 2021-02-18
      相关资源
      最近更新 更多