有一个别人托管的maven项目,里面有src/main
目录和src/test
目录。 src/test
dir 包含一个以 Test
结尾的 java 文件,我就称之为 ATest.java
,以及类(class) ATest
仅包含一个 static void main 函数。就像
//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
当我输入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
并添加@Test
描述符位于 main 函数之前,但它会给出错误,因为 main 是静态的并且它接受参数。然后我尝试移动BTester.main
和CTester.main
到另一个函数testmain
并添加@Test
之前testmain
,但是编译时会出现错误,说我有未捕获的异常,即使我添加throws 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>
但是还是不行。
我错过了什么?我对Java不太熟悉,对maven也不太熟悉,所以我现在真的需要一些帮助。谢谢。
最佳答案
- 尝试将您的测试类放在/src/test/java 下。
- 您需要您的测试方法。不要使用 main 作为使用 JUNIT 的测试方法 请参阅此引用:http://www.vogella.com/tutorials/JUnit/article.html
关于java - 运行maven test命令时没有测试运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43833127/