java junit 找不到类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15548580/
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
junit cannot find class
提问by n as
I am able to run the sample tests supplied with junit (from any directory). I would think that this suggests my installation of junit is perfectly fine.
I assume, to compile a junit test, it is no different from any other java file, namely: javac fileName.java
My test file (.java and resulting .class) lives in: c:\parent\child. Obviously, in order to compile the .java file, I have a package statement on the first line: package parent.child followed by the all-important: import junit.framework.TestCase; After this, there is a public class fileName definition extends TestCase {}. The file compiles without any warnings or errors.
when I attempt (in the c:\parent\child directory where fileName exists, both the .java and .class):
我能够运行 junit 提供的示例测试(从任何目录)。我认为这表明我安装的 junit 非常好。
我假设,要编译一个 junit 测试,它与任何其他 java 文件没有什么不同,即: javac fileName.java
我的测试文件(.java 和生成的 .class)位于:c:\parent\child。显然,为了编译 .java 文件,我在第一行有一个 package 语句: package parent.child 后跟最重要的: import junit.framework.TestCase; 在此之后,有一个公共类 fileName 定义 extends TestCase {}。该文件编译时没有任何警告或错误。
当我尝试时(在文件名存在的 c:\parent\child 目录中,.java 和 .class):
java org.junit.runner.JUnitCore parent.child.fileName, I get the following: JUnit version 4.1 Could not find class: parent.child.fileName Time: 0 OK (0 tests)
java org.junit.runner.JUnitCore parent.child.fileName,我得到以下信息:JUnit 4.1 版找不到类:parent.child.fileName 时间:0 OK(0 次测试)
Even if I drop parent.child altogether from the command, it makes no difference.
My CLASSPATH environment variable is set to: c:\parent\junit\junit-4.1.jar;c:\parent\junit;.
If I trying running with -cp c:\ or c:\parent\child or anything else, I still get the error.
即使我从命令中完全删除 parent.child,也没有区别。
我的 CLASSPATH 环境变量设置为:c:\parent\junit\junit-4.1.jar;c:\parent\junit;。
如果我尝试使用 -cp c:\ 或 c:\parent\child 或其他任何东西运行,我仍然会收到错误消息。
回答by Ernest Friedman-Hill
Java package names are actually part of the class name, and they're also used in a specific way to find *.class
files. If Java wants to find a class named parent.child.fileName
, it's going to look for a file named parent/child/fileName.class
-- i.e., it's going to look for the file in a directory named child
in a directory named parent
. You have to specify the class path such that the parent
directory will be found, something like
Java 包名实际上是类名的一部分,它们也以特定方式用于查找*.class
文件。如果 Java 想要找到一个名为 的类parent.child.fileName
,它将寻找一个名为的文件parent/child/fileName.class
——也就是说,它将child
在名为parent
. 您必须指定类路径parent
以便找到目录,例如
java -cp "c:/junit/junit.jar;c:/" org.junit.runner.JUnitCore parent.child.fileName
The class path (-cp) argument has to specify all the places that Java should look for classes, including jar files like junit.jar
, which I've imagined is located in a directory called junit
. The semicolon ";" is used to separate entries on the class path (assuming you're using Windows; on real computers it's a ":" instead.) The "c:/" entry in the class path is the one that will be used to find the parent
directory and thus your class.
类路径 (-cp) 参数必须指定 Java 应该查找类的所有位置,包括 jar 文件junit.jar
,我认为它位于名为junit
. 分号“;” 用于分隔类路径上的条目(假设您使用的是 Windows;在真实计算机上,它是一个“:”。)类路径中的“c:/”条目将用于查找parent
目录因此你的班级。
回答by ArturSkowronski
You should use JUnit Runner to run test outside IDE, not only compile it.
您应该使用 JUnit Runner 在 IDE 之外运行测试,而不仅仅是编译它。
java -cp lib/junit.jar;sw.jar org.junit.runner.JUnitCore your.package.fileName
update this line to yours project structure.
将此行更新为您的项目结构。
回答by hiflyer
In general it's so much easier to run unit tests using a build system like ant/maven/gradle or from within your IDE (IntelliJ/eclipse/Netbeans)
一般来说,使用诸如 ant/maven/gradle 之类的构建系统或从您的 IDE (IntelliJ/eclipse/Netbeans) 中运行单元测试要容易得多