运行Maven测试命令时无测试运行 [英] No test run when running maven test command
问题描述
有一个由其他人托管的Maven项目,该项目具有src/main
目录和src/test
目录. src/test
目录包含一个以Test
结尾的Java文件,我将其称为ATest.java
,而类ATest
仅包含一个静态void main 函数.就像
There is a maven project hosted by someone else, which has src/main
directory and src/test
directory. The src/test
dir contains a java file ending with Test
, I'll just call it ATest.java
, and the class ATest
only contains a static void main function. It is like
//omitted
public class ATest {
public static void main(String[] args) throws Exception {
BTester.main(null);
CTester.main(null);
}
}
其中BTester
和CTester
都是目录下的文件
src/main/java/<package_path>/tester
where BTester
and CTester
are both files under directory
src/main/java/<package_path>/tester
当我键入mvn clean test
时,它会显示
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
我尝试了以下方法:
-
import org.junit.Test
并在main函数之前添加@Test
描述符,但是由于main是静态的并且接受参数,因此会出现错误.然后,我尝试将BTester.main
和CTester.main
移至另一个函数testmain
,并在testmain
之前添加@Test
,但是在编译时会导致错误,说即使我添加了throws Exception
,我也有未捕获的异常?
import org.junit.Test
and add@Test
descriptor ahead of the main function, but it gives error because main is static and it accepts parameters. Then I tried to moveBTester.main
andCTester.main
to another functiontestmain
and add@Test
beforetestmain
, but it causes errors when compiling, saying that I have uncaught exception, even if I addthrows Exception
?
使用maven-surefire-plugin
,这是我在pom.xml
文件中执行的操作
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<includes>
<include>ATest.java</include>
</includes>
</configuration>
</plugin>
use the maven-surefire-plugin
, here is how I did it in the pom.xml
file
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<includes>
<include>ATest.java</include>
</includes>
</configuration>
</plugin>
但是它仍然不起作用.
我想念什么?我对Java不太熟悉,对maven也不很熟悉,所以我现在确实需要一些帮助.谢谢.
What did I miss? I'm not very familiar with Java, not with maven, so I really need some help now. Thanks.
推荐答案
- 尝试将测试类放在/src/test/java下.
- 您需要测试方法.不要使用main作为使用JUNIT的测试方法 请参阅以下参考: http://www.vogella.com/tutorials/JUnit/article. html
- Try to place your test class under /src/test/java.
- You need your test method. Do not using main for test method using JUNIT See this reference: http://www.vogella.com/tutorials/JUnit/article.html
这篇关于运行Maven测试命令时无测试运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!