首页 > 解决方案 > 如何指定要测试的不同类名模式?

问题描述

我又要回到 Java 世界了……

我正在学习一个教程(https://spring.io/guides/gs/spring-boot/),当我使用 运行测试时mvn test,没有一个测试HelloControllerIT正在运行。似乎只考虑以“Test”结尾的类。我确信有一种方法可以添加其他模式,以便HelloControllerIT包含在内。

在哪里可以找到有关此主题的更多信息?

这看起来很简单,所以我可能没有在搜索中使用正确的关键字(例如,'java spring boot test pattern')。

更新

感谢 Yug Singh 的回答,我能够想出一个我觉得很好的解决方案。我将此添加到我的pom.xml文件中,现在我可以运行与集成测试分开的单元测试。

我忘了简介...

+    <profiles>
+        <profile>
+            <id>integration</id>
+            <build>
+                <plugins>
+                    <plugin>
+                        <groupId>org.apache.maven.plugins</groupId>
+                        <artifactId>maven-surefire-plugin</artifactId>
+                        <configuration>
+                            <includes>
+                                <include>**/*IT.java</include>
+                            </includes>
+                        </configuration>
+                    </plugin>
+                </plugins>
+            </build>
+        </profile>
+    </profiles>

运行单元测试

mvn test

运行集成测试

mvn test -Pintegration

参考资料(堆栈溢出):

标签: javaspring-boottesting

解决方案


您可以在 pom.xml 文件中尝试以下内容:

<build>
  <testSourceDirectory>src/main/java</testSourceDirectory>
  <plugins>
    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <version>2.16</version>
       <configuration>
          <includes>
             <include>**/*.java</include>
          </includes>
       </configuration>
    </plugin>
 </plugins>

恕我直言,您应该只将测试放在测试包中,因为这是惯例。


推荐阅读