如何 运行 当测试文件不在 mavenise 结构中时在 Maven 中进行 Junit 测试

How to run Junit Test in Maven when a test file is not in mavenise structure

我有这么多 java 个文件,而那些 java 个文件 test.java 不在 src/test/java 结构中。 例如:我在 src/java/abc 中有一个名为 abc.java 的 java 文件,但是这个名为 abcTest.java 的 java 文件的测试文件在 src/java/junit/abc 中. 那么我将如何通过 Maven pom.xml 对这个 java 文件进行 junit 测试,因为 Maven 希望 src/main/java 中的正常 java 文件和 src/test/java 中的测试文件 所以如何我会通过maven做测试吗?

我在 pom.xml 中添加了 junit 依赖项和 surefire 插件。

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

surefire 插件是

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.10</version>
               <configuration> 
               <testSourceDirectory>${basedir}/../src/java/junit/*.java</testSourceDirectory> 
               <testClassesDirectory>${project.build.directory}/classe‌​s/</testClassesDirectory>
<includes>
            <include>abcTest.java</include>
          </includes>
                </configuration> 
            </plugin>

我已经给出了 mvn test 命令,但它仍然在日志中给出零测试。谁能帮我解决如何通过 Maven 运行 junit test.java 文件的问题?

假设你不能真正了解项目的结构,你将不得不自定义 Surefire 插件。

添加 Junit 依赖项是不够的 - 它只允许编译您的测试 类。

所以,您的方向是正确的 - 您必须自定义 Maven 的 Surefire 插件,负责 运行测试。

现在,为了使 Surefire 插件 运行 结束不以 *Test.java 结尾的测试,您必须更改包含。也就是说,您必须指定正则表达式来反映您希望 运行.

的测试名称

This page 描述的就是这个。

我已经在我的 pom.xml basedirectory 标记和 junit 测试用例 运行 成功之后添加了下面这两行。现在我是 运行 mvn install 并且我在 test.java 中的所有测试用例都是 运行 尽管我没有类似 maven 的结构。

<testSourceDirectory>${basedir}/../src/java/junit</testSourceDirectory> 
        <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>

然后我添加了 surefire 插件,这就是我所有的 junit 测试用例 运行。