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):
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)
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.
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 -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.
You should use JUnit Runner to run test outside IDE, not only compile it.
java -cp lib/junit.jar;sw.jar org.junit.runner.JUnitCore your.package.fileName
update this line to yours project structure.
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)