从DOS命令行运行JUnit测试时出错 [英] Error when running JUnit Tests from DOS Command line
问题描述
我的项目正在Windows 2008 Server上运行.
My projects is running on a Windows 2008 Server.
我的项目源代码位于以下文件夹中:
My project source code sit in the following folders:
com.company.division.dao
com.company.division.entity
com.company.division.main
com.company.division.junit
我的junit测试类位于文件夹C:\ Sample \ com \ company \ division \ junit中. 我正在使用JUnit 4.11
My junit test class are in the folder C:\Sample\com\company\division\junit. I am using JUnit 4.11
当我从Eclipse运行此程序时,它运行正常.
When I run this from Eclipse it runs fine.
我已经在DOS命令行中尝试了以下方法:
I have tried the following from DOS command line:
首先我CD C:\ Sample \ com \ company \ division \ junit \
First I cd C:\Sample\com\company\division\junit\
从C:\ Sample \ com \ company \ division \ junit>运行已编译的EventTests.class文件的位置,我运行:
From C:\Sample\com\company\division\junit> where the compiled EventTests.class file exists I run:
java -classpath "C:\Sample\lib\junit.jar;" org.junit.runner.JUnitCore com.company.division.junit.EventTests
或
java -cp "C:\Sample\lib\junit.jar;" org.junit.runner.JUnitCore com.company.division.junit.EventTests
或
java -cp ".;C:\Sample\lib\junit.jar;" org.junit.runner.JUnitCore com.company.division.junit.EventTests
但是似乎没有任何效果.我有一个例外:
But nothing seems to work. I get an exception:
JUnit version 4.11
Could not find class: com.company.division.junit.EventTests
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
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)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at org.junit.runner.Request.classes(Request.java:75)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:96)
at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47)
at org.junit.runner.JUnitCore.main(JUnitCore.java:40)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader$1.run(Unknown Source)
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)
... 18 more
已编辑-新更新
因此,从其他线程之一中,我弄清楚了命令eclipse是什么用于运行单元测试. Eclipse使用的是javaw.exe,我用java.exe工具替换了javaw. 这是命令,
Edited - New update
So from one of the other threads I figured out what the command eclipse was using to run the unit tests. Eclipse was using javaw.exe, I replaced javaw with java.exe tool. Here is the command,
C:\Java\64bit_Browser_Plugin\bin\java.exe -Dfile.encoding=Cp1252 -classpath C:\working_dir\bin;C:\working_dir\lib\mysql-connector-java-5.0.8-bin.jar;C:\working_dir\lib\ojdbc14.jar;C:\working_dir\lib\spring-beans-3.2.4.RELEASE.jar;C:\working_dir\lib\spring-context-3.2.4.RELEASE.jar;C:\working_dir\lib\spring-core-3.2.4.RELEASE.jar;C:\working_dir\lib\spring-expression-3.2.4.RELEASE.jar;C:\working_dir\lib\spring-jdbc-3.2.4.RELEASE.jar;C:\working_dir\lib\spring-tx-3.2.4.RELEASE.jar;C:\working_dir\lib\commons-logging-1.1.3.jar;C:\working_dir\lib\nzjdbc.jar;C:\working_dir\lib\junit.jar;C:\working_dir\lib\spring-batch-core-2.2.5.RELEASE.jar;C:\working_dir\lib\spring-batch-infrastructure-2.2.5.RELEASE.jar;C:\working_dir\lib\hamcrest-core-1.3.jar;C:\working_dir\lib\log4j-1.2.14.jar;C:\working_dir\lib\mail.jar;C:\working_dir\lib\spring-context-support-4.0.1.RELEASE.jar;/C:/eclipse/configuration/org.eclipse.osgi/bundles/167/1/.cp/;/C:/eclipse/configuration/org.eclipse.osgi/bundles/166/1/.cp/ org.eclipse.jdt.internal.junit.runner.RemoteTestRunner -version 3 -port 54536 -testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader -loaderpluginname org.eclipse.jdt.junit4.runtime -classNames com.omnitracs.fra.junit.EventTests
我仍然遇到异常:
Could not connect to: : 54536
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.connect(Remote
TestRunner.java:570)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTest
Runner.java:381)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTes
tRunner.java:197)
我想念什么?
推荐答案
尝试在类路径中包含hamcrest-core.jar.
Try including hamcrest-core.jar in the classpath.
这篇关于从DOS命令行运行JUnit测试时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!