java - 如何使用 surefire 插件在自定义文件夹结构中运行测试

标签 java maven testing junit maven-surefire-plugin

我有一个定制的 Eclipse Studio 项目。对于单元测试,我们正在创建一个测试文件 -

  TestJavaSrc/demoTest.java

现在这个 TestJavaSrc 文件夹与 POM.xml 处于同一级别 这是 POM.xml -

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo.test</groupId>
    <artifactId>demo</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
            </plugin>
        </plugins>
    </build>
</project>

现在,当我将此项目作为 mvn test 运行时,它无法找到任何测试文件。 而且,当我在命令行上运行以下命令时 -

 mvn "-Dtest=TestJavaSrc/DemoTest.java" test

它给我错误-

 Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test
 (default-test) on project demo: No tests were executed! 
 (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]

此外,我的类路径中有 JUnit4

有什么我遗漏的吗?

最佳答案

在 Maven 中 standard directory layout ,用Java编写的单元测试应该位于src/test/java下:

The src directory contains all of the source material for building the project, its site and so on. It contains a subdirectory for each type: main for the main build artifact, test for the unit test code and resources, site and so on.

因此,Surefire 插件将执行恰好位于该文件夹中的测试;这可以通过 testSourceDirectory 配置范围。请注意,此目录是包含所有测试的基本源目录。这意味着如果该基目录是 somedir 并且有一个名为 DemoTest.java 的 Java 类,声明在包 foo.bar 中,那么磁盘上的文件必须位于 somedir/foo/bar/DemoTest.java

这是参数 test 的位置您正在(误)使用输入:它选择 Java 类以仅按其名称执行;不是通过其包声明,也不是通过文件在磁盘上的位置。

我建议您将测试类放在该标准目录中,但如果您真的想这样做,有多种选择:

  1. 如果您想要一个位于 Maven 项目的基本目录的测试源目录,即 TestJavaSrc,那么您可以在 POM 中配置它:

    <build>
      <testSourceDirectory>${project.basedir}/TestJavaSrc</testSourceDirectory>
    </build>
    
  2. 如果你想为那个单独的测试创建一个特殊的目录,也就是说你想保留默认的 src/test/java,但又想额外考虑这个新的源目录,然后你可以使用add-test-source Build Helper Maven 插件的目标:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>1.12</version>
      <executions>
        <execution>
          <id>add-test-source</id>
          <goals>
            <goal>add-test-source</goal>
          </goals>
          <configuration>
            <sources>
              <source>TestJavaSrc</source>
            </sources>
          </configuration>
        </execution>
      </executions>
    </plugin>
    

请注意,这两种方法都意味着直接位于该文件夹下的 demoTest.java 文件将位于默认包中,因此它不能有 package声明。

关于java - 如何使用 surefire 插件在自定义文件夹结构中运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40610683/

相关文章:

Java instanceof 在我的情况下不起作用

java - Dropwizard:java.lang.IllegalStateException:无法获取记录器上下文

java - maven checkstyle无法找到插入包含配置的位置

javascript - 谷歌 postman : How to get properties of an Array

testing - 生成在 JMeter 中只出现一次的随机数

java - 亚利桑那时区夏令时

java - 在收据上使用 Tesseract 时如何获得更好的效果?

java - 执行jar文件时出现IIOException

java - 在 SonarQube 中编写自己的 Java 自定义规则的初学者指南

testing - golang 单元测试 : inner function parameters