简体   繁体   English

从DOS命令行运行JUnit测试时出错

[英]Error when running JUnit Tests from DOS Command line

My projects is running on a Windows 2008 Server. 我的项目在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

My junit test class are in the folder C:\\Sample\\com\\company\\division\\junit. 我的junit测试类位于文件夹C:\\ Sample \\ com \\ company \\ division \\ _unit中。 I am using JUnit 4.11 我正在使用JUnit 4.11

When I run this from Eclipse it runs fine. 当我从Eclipse运行它时运行正常。

I have tried the following from DOS command line: 我从DOS命令行尝试了以下内容:

First I cd C:\\Sample\\com\\company\\division\\junit\\ 首先我cd C:\\ Sample \\ com \\ company \\ division \\ junit \\

From C:\\Sample\\com\\company\\division\\junit> where the compiled EventTests.class file exists I run: 从C:\\ Sample \\ com \\ company \\ division \\ _unit>中编译的EventTests.class文件存在,我运行:

java  -classpath "C:\Sample\lib\junit.jar;" org.junit.runner.JUnitCore com.company.division.junit.EventTests

OR 要么

java  -cp "C:\Sample\lib\junit.jar;" org.junit.runner.JUnitCore com.company.division.junit.EventTests

OR 要么

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

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用来运行单元测试的命令。 Eclipse was using javaw.exe, I replaced javaw with java.exe tool. Eclipse正在使用javaw.exe,我用java.exe工具替换了javaw。 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

I am still getting an exception: 我仍然得到一个例外:

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)

What am I missing? 我错过了什么?

Try including hamcrest-core.jar in the classpath. 尝试在类路径中包含hamcrest-core.jar。

https://github.com/junit-team/junit/wiki/Download-and-Install https://github.com/junit-team/junit/wiki/Download-and-Install

Changing directories to where your test is located is a mistake. 将目录更改为测试所在的位置是错误的。 The java program will start from the classpath you give it and assume the package directory structure starts from there, so if you are in C:\\Sample\\com\\company\\division\\junit\\ and you give the java command -cp = "." java程序将从你给它的类路径开始,并假设包目录结构从那里开始,所以如果你在C:\\Sample\\com\\company\\division\\junit\\ ,你给java命令-cp = "." then it will look for the class com.company.division.junit.EventTests in C:\\Sample\\com\\company\\division\\junit\\com\\company\\division\\junit\\ . 然后它将在C:\\Sample\\com\\company\\division\\junit\\com\\company\\division\\junit\\查找类com.company.division.junit.EventTests

Also if you don't include your classes in the classpath (your last example is the only one to include them) then the java exe won't find them. 此外,如果您不在类路径中包含类(您的最后一个示例是唯一包含它们的示例),那么java exe将找不到它们。

You need to add the current directory to the classpath, first changing directories so that the top of the package structure is at your current directory: 您需要将当前目录添加到类路径中,首先更改目录,以使包结构的顶部位于当前目录中:

cd c:\Sample
java -cp ".;lib\*.jar" org.junit.runner.JUnitCore com.company.division.junit.EventTests 

This way . 这样. points to where your packages are defined. 指向您的包的定义位置。

If you would rather use absolute paths then try 如果您更愿意使用绝对路径,请尝试

java -cp "C:\Sample;C:\Sample\lib\*.jar" org.junit.runner.JUnitCore com.company.division.junit.EventTests 

You can use the * as a wildcard to include multiple jars starting with jdk 6. 您可以使用*作为通配符来包含以jdk 6开头的多个jar。

For the future, consider using the directory layout used by default in Maven. 对于将来,请考虑使用Maven中默认使用的目录布局。 Keeping tests separate from code is typically a good thing. 将测试与代码分开通常是一件好事。 I wonder if part of this problem isn't that test-only dependencies are located in a different spot than other libraries. 我想知道这个问题的一部分是不是仅测试依赖项位于与其他库不同的位置。

Right-click on your project in Eclipse and find the menu Build Path -> Configure Build Path , and make sure you know where Eclipse is getting these jars from. 在Eclipse中右键单击您的项目,找到菜单Build Path -> Configure Build Path ,并确保您知道Eclipse从哪里获取这些jar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM