如何从命令行在JUnit中运行测试用例?

时间:2011-03-02 16:46:07

标签: java junit junit4

我正在尝试使用此命令从命令行运行JUnit测试用例:

F:\>java org.junit.runner.JUnitCore org.junit4.9b2.junit.SimpleTest

但是我收到了这个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore

Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: org.junit.runner.JUnitCore.  Program will exit.

有什么问题?

1 个答案:

答案 0 :(得分:8)

显然你需要在类路径上使用junit: - )

java -cp path/to/junit.jar:path/to/local/classes org.junit.runner.JUnitCore \
         org.junit4.9b2.junit.SimpleTest

(在Windows平台上将:替换为;