views:

1145

answers:

5

I'm trying to run the sample tests that come with junit4.7 and having some difficulty.

java is respecting my CLASSPATH:

me@dinosaurhunter ~/Desktop> export CLASSPATH=
me@dinosaurhunter ~/Desktop> echo $CLASSPATH

me@dinosaurhunter ~/Desktop> java junit.textui.TestRunner junit.samples.AllTests
Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/TestRunner
me@dinosaurhunter ~/Desktop> source /etc/profile 
me@dinosaurhunter ~/Desktop> echo $CLASSPATH
:/Library/Java/Extensions/junit/:/Library/Java/Extensions/junit/junit.jar
me@dinosaurhunter ~/Desktop> java junit.textui.TestRunner junit.samples.AllTests
Exception in thread "main" java.lang.NoClassDefFoundError: junit/framework/Test
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:164)
    at junit.runner.BaseTestRunner.loadSuiteClass(BaseTestRunner.java:207)
    at junit.runner.BaseTestRunner.getTest(BaseTestRunner.java:100)
    at junit.textui.TestRunner.start(TestRunner.java:179)
    at junit.textui.TestRunner.main(TestRunner.java:138)

but as you can see, it's unable to find junit/framework/Test... I looked in the /Library/Java/Extensions/junit/junit.jar and it is included, though.

/Library/Java/Extensions/junit/junit.jar is a symlink. Is that okay?

A: 

It is possible that using a symlink is causing your problem. Trying adding the jar that symlink is pointing to and try again.

However looking at the java man page for mac os x there doesn't seem to be such a restriction.

hhafez
I did it without the symlink and I'm still getting the exact same issue.
ashgromnies
A: 

Some things to investigate:

Is the junit.jar file in the location you specify, and is it readable? Is CLASSPATH exported by /etc/profile? Does it work when you set "-cp $CLASSPATH" instead on the command line? Try removing the leading colon in the classpath -- should not be there.

Sean Owen
Yes, the junit.jar exists there with permissions 755. CLASSPATH is exported by /etc/profile. Unsetting CLASSPATH and using -cp gives me the same results. The leading colon doesn't seem to affect it either.
ashgromnies
+4  A: 

Ok, I just downloaded JUnit 4.7, unpacked the zip file, changed directory into the folder and ran the following command successfully:

$ java -cp .:junit-4.7.jar junit.textui.TestRunner junit.samples.AllTests 
.........................................
.........................................
.........................................
.......
Time: 3.255

OK (130 tests)

That was on OSX.

I think in your example, your classpath is a little messed up. Try this:

.:/Library/Java/Extensions/junit:/Library/Java/Extensions/junit/junit.jar

See the differences? I added the . (current directory) and I removed the trailing slash from the junit directory.

UPDATE: I just tested with a symlink and that appears to work too:

$ ln -s junit-4.7.jar junit.jar
$ java -cp .:junit.jar junit.textui.TestRunner junit.samples.AllTests 
.........................................
.........................................
.........................................
.......
Time: 2.569

OK (130 tests)
Asaph
+3  A: 

Did you build the JAR yourself? This looks like the JAR is built without annotation.

If you compile the JUnit with

  javac -proc none ...

You will get this error.

ZZ Coder
+2  A: 

Try putting the junit JAR file directly into the Extensions directory instead of creating a subdirectory for it. I just copied the junit-4.6.jar into the /Library/Java/Extenstions directory and executed the TestRunner class with no issues

% java junit.textui.TestRunner
Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class

Deleting the library from the Extensions directory results in the expected exception

Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/TestRunner
John Haager