2011-11-18 134 views
1

我试图从命令行运行单个JUnit测试,但出现错误。 我可以成功编译JUnit测试,并在正确的位置创建类文件。 但是当我尝试使用运行它:从命令行运行JUnit测试时出错

C:\Program Files\Java\jdk1.7.0_01\bin>java org.junit.runner.JUnitCore C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\src\com\org\tests\Nav.class 

我得到的错误:

JUnit version 4.8.1 
Could not find class: C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\src\com\org\tests\Nav.class 
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDesc 
ribbing 

我不知道为什么它是不能够找到类,即使它在说存在位置。

回答

2

你需要在命令行指定的类的名称,而不是文件名:

java org.junit.runner.JUnitCore com.org.tests.Nav 

从Javadoc文档JUnitCore

JUnitCore is a facade for running tests. It supports running JUnit 4 tests, JUnit 3.8.x tests, and mixtures. To run tests from the command line, run java org.junit.runner.JUnitCore TestClass1 TestClass2 .... For one-shot test runs, use the static method runClasses(Class[]). If you want to add special listeners, create an instance of org.junit.runner.JUnitCore first and use it to run the tests.

,你将需要添加的bin目录(注意不是src)以及命令行的类路径。这可能看起来像:

java -cp C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\bin org.junit.runner.JUnitCore com.org.tests.Nav 
+0

谢谢马修。我改变了你的路径,现在它工作正常。非常感谢 。 –

相关问题