java JUnit:4.8.1“找不到类”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2472408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:21:16  来源:igfitidea点击:

JUnit: 4.8.1 "Could not find class"

javajunit4

提问by Patrick

Ok, I am like other and new to jUnit and having a difficult time trying to get it working. I have searched the forum but the answers provided; I am just not getting. If anyone out there could lend me a hand I would greatly appreciate it.

好的,我和其他人一样,也是 jUnit 的新手,并且很难让它工作。我已经搜索了论坛,但提供了答案;我只是没有得到。如果有人能帮我一把,我将不胜感激。

Let me provide the basics: OS: mac OS X.6

让我提供基础知识: 操作系统:mac OS X.6

export JUNIT_HOME="/Developer/junit/junit4.8.1"
export CVSROOT="/opt/cvsroot"
export PATH="/usr/local/bin:/usr/local/sbin:/usr/localmysql/bin:/opt/PalmSDK/Current/bin/:/usr/local/mysql/bin:$PATH:$JUNIT_HOME:$CVSROOT"
export CLASSPATH="$CLASSPATH:$JUNIT_HOME/junit-4.8.1.jar:$JUNIT_HOME"

I can compile a test class from a java file, however when I try to then run the test

我可以从 java 文件编译测试类,但是当我尝试运行测试时

java org.junit.runner.JUnitCore MyTest.class 

I get the following:

我得到以下信息:

JUnit version 4.8.1
Could not find class: MyTest.class

Time: 0.001

OK (0 tests)

Now I have been in the directory with the MyTest.classwhich is just somewhere in my file system, I tried moving the source folder to the junitfolder and the junit/junit4.8.1folder and the same result. I cannot even run the tests that came with junit.

现在我已经MyTest.class在我的文件系统中某处的目录中,我尝试将源文件夹移动到junit文件夹和junit/junit4.8.1文件夹中,结果相同。我什至无法运行 junit 附带的测试。

回答by Jonathan Feinberg

Is MyTestreally in the default package? If not, then you need to give the entire package-qualified name. In other words, if MyClass has a statement

是否MyTest真的在默认的包?如果没有,那么您需要提供整个包限定名称。换句话说,如果 MyClass 有一个声明

package com.myself;

and lives in

并住在

/myproject/src/com/myself/MyClass.java

and you compiled into

你编译成

/myproject/classes

then /myproject/classesmust be on your CLASSPATHand you must

那么/myproject/classes必须在你的CLASSPATH身上,你必须

java org.junit.runner.JUnitCore com.myself.MyTest

Come to think of it, I see now that you're appending .classto the class name, so even if it's in the default package, you should just say

想想看,我现在看到你附加.class到类名,所以即使它在默认包中,你也应该说

java org.junit.runner.JUnitCore MyTest

回答by ezmia

Remove .class from MyTest.class i.e. java org.junit.runner.JUnitCore MyTest

从 MyTest.class 中删除 .class,即 java org.junit.runner.JUnitCore MyTest

回答by Kathy Van Stone

It is not having problems finding JUnit -- it is finding that okay. It can't find MyClass, so the directory of that class (given that it is packageless) needs to be in the classpath.

找到 JUnit 没有问题——它找到了没问题。它找不到MyClass,因此该类的目录(假设它是无包的)需要在类路径中。