无法从命令行运行简单的 JUnit4 测试
我有一个 ant 构建,它编译 src...test
中的 JUnit 测试文件并将其复制到 build/timex...com/visualpatterns/timex/test
(我在下面写了完整的路径名)。但无论如何,我都无法让 JUnit 来运行这个东西。谁能指出我正确的方向(我已经浪费了几个小时)?我的 CLASSPATH 似乎没问题(JUnit 运行),但它找不到类文件...
$ echo $CLASSPATH
**.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\junit4.8.2\junit-4.8.2.jar;C:\Program Files\junit4.8.2**
Me@My-PC /cygdrive/c/java/Code/Command_Line/Projects/timex
$ **java org.junit.runner.JUnitCore**
JUnit version 4.8.2
Time: 0.001
OK (0 tests)
Me@My-PC /cygdrive/c/java/Code/Command_Line/Projects/timex
$ **java -cp build/timex/WEB-INF/classes org.junit.runner.JUnitCore com.visualpatterns.timex.test.SimpleTest**
java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: org.junit.runner.JUnitCore. Program will exit.
Exception in thread "main"
/cygdrive/c/java/Code/Command_Line/Projects/timex/
------------------------------------------------------
**build/timex/WEB-INF/classes/com/visualpatterns/timex/test/***SimpleTest.class*
SimpleTest.java
package com.visualpatterns.timex.test;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class SimpleTest
{
int value1 = 2, value2 = 3, expectedResult = 5;
@Test
public void addSuccess()
{
assertTrue(value1 + value2 == expectedResult);
}
@Test
public void addFail()
{
assertTrue(value1 - value2 == expectedResult);
}
}
I have an ant build that compiles a JUnit test file file insrc...test
and copies it to build/timex...com/visualpatterns/timex/test
(I've written the complete path name below). But for the life of me, I can't get JUnit to run the thing. Can anyone point me in the right direction (I've wasted hrs on this)? My CLASSPATH
seems to be ok (JUnit runs), but it can't find the class file...
$ echo $CLASSPATH
**.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\junit4.8.2\junit-4.8.2.jar;C:\Program Files\junit4.8.2**
Me@My-PC /cygdrive/c/java/Code/Command_Line/Projects/timex
$ **java org.junit.runner.JUnitCore**
JUnit version 4.8.2
Time: 0.001
OK (0 tests)
Me@My-PC /cygdrive/c/java/Code/Command_Line/Projects/timex
$ **java -cp build/timex/WEB-INF/classes org.junit.runner.JUnitCore com.visualpatterns.timex.test.SimpleTest**
java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: org.junit.runner.JUnitCore. Program will exit.
Exception in thread "main"
/cygdrive/c/java/Code/Command_Line/Projects/timex/
------------------------------------------------------
**build/timex/WEB-INF/classes/com/visualpatterns/timex/test/***SimpleTest.class*
SimpleTest.java
package com.visualpatterns.timex.test;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class SimpleTest
{
int value1 = 2, value2 = 3, expectedResult = 5;
@Test
public void addSuccess()
{
assertTrue(value1 + value2 == expectedResult);
}
@Test
public void addFail()
{
assertTrue(value1 - value2 == expectedResult);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在第二次执行中,您将使用
-cp
更改类路径。如果您想向类路径添加新条目,这是可以的,但在您的情况下,您要替换获得 junit 库的旧类路径。这就是为什么它无法再找到 junit 类(java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
)。您必须使用类似的东西:另请参阅 PATH 和 CLASSPATH和设置类路径(Google 首次点击)。
In your second execution you are changing the classpath with
-cp
. This is okay if you want to add new entries to the classpath but in your case you are replacing the old classpath which got the junit library. That's why it cannot found the junit class anymore (java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
). You have to use something like that:See also PATH and CLASSPATH and Setting the class path (first google hits).
您不能像那样从命令行运行 JUnit。这些测试没有
main
方法,因此不可执行。如果您使用 Ant 编译源代码,那么您也应该使用 Ant 来运行测试。
请参阅 Ant 手册的 JUnit 部分。
You don't run JUnit from the command line like that. The tests don't have a
main
method, so are not executable.If you used Ant to compile the source, then you should use Ant to run the tests also.
See the JUnit section of the Ant manual.
您还可以仅使用 Eclipse 来运行 Junit 测试。它比使用 Ant 更快。然而后者更便携。在 Eclipse 中,只需输入键盘快捷键 Alt+Shift+X+T 即可运行单元测试。
You can also just use Eclipse to run your Junit tests. It's faster than using Ant. The latter however is more portable. In Eclipse simply key in the keyboard shortcut Alt+Shift+X+T to run your unit test.
谢谢大家!! (并特别感谢 Progman)。问题是在类参数周围加上单引号(我使用的是 Cygwin)。
Me@My-PC /cygdrive/c/java/Code/Command_Line/Projects/timex
$ java -cp '$CLASSPATH;C:\Program Files\junit4.8.2\junit-4.8.2.jar;C:\Program Files\junit4.8.2;build/timex/WEB-INF/classes' org.junit. runner.JUnitCore com.visualpatterns.timex.test.SimpleTest
就 Eclipse/Ant 而言,这太简单了;-)。我认为当您从命令行开始而不是让自动化完成这一切时,您会学到更多,有点像阅读地图与使用 GPS 一样。一旦我开始运行这个小网络架构/应用程序,我将切换到 Eclipse 以节省时间,但就我所知,我更愿意多学一点(并犯一些愚蠢的错误)。
Me@My-PC /cygdrive/c/java/Code/Command_Line/Projects/timex
$ java -cp '$CLASSPATH;C:\Program Files\junit4.8.2\junit-4.8.2.jar;C:\Program Files\junit4.8.2;build/timex/WEB-INF/classes' org.junit. runner.JUnitCore com.visualpatterns.timex.test.SimpleTest
Thanks all!! (and a special shout-out to Progman). The problem was putting single quotes around the class argument (I'm using Cygwin).
Me@My-PC /cygdrive/c/java/Code/Command_Line/Projects/timex
$ java -cp '$CLASSPATH;C:\Program Files\junit4.8.2\junit-4.8.2.jar;C:\Program Files\junit4.8.2;build/timex/WEB-INF/classes' org.junit.runner.JUnitCore com.visualpatterns.timex.test.SimpleTest
As far as Eclipse/Ant goes, that's too easy ;-). I think you learn more when you start with the command line instead of having automation doing it all, kinda like reading a map vs. using a GPS. Once I get this little web arch/app going, I'll switch to Eclipse to save time, but for know I prefer to learn a bit more (and make dumb mistakes).
Me@My-PC /cygdrive/c/java/Code/Command_Line/Projects/timex
$ java -cp '$CLASSPATH;C:\Program Files\junit4.8.2\junit-4.8.2.jar;C:\Program Files\junit4.8.2;build/timex/WEB-INF/classes' org.junit.runner.JUnitCore com.visualpatterns.timex.test.SimpleTest