Similar to other questions on this subject, I am having problems getting JUnit 4.11 to recognize my class.
Similar to the question found here, I am using algs4.jar to write a class, Percolation, which is all in the default package. Just as in the post I linked, I too had difficulty importing algs4 into my class to use the required WeightedWuickUnionUF class found within. I was finally able to compile the program with the following setup.
> echo $CLASSPATH
/Users/dj/Library/Java/Extensions/algs4.jar:
/Users/dj/Library/Java/Extensions/hamcrest-core-1.3.jar:
/Users/dj/Library/Java/Extensions/junit-4.11.jar:
/Users/dj/Library/Java/Extensions/stdlib.jar
Percolation.java
import java.util.Arrays;
import algs4.*;
public class Percolation {
private WeightedQuickUnionUF union_find_ds;
...
}
test_Percolation.java
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.Assert.*;
@RunWith(JUnit4.class)
public class test_Percolation{
private static Percolation perc;
@BeforeClass
public static void testSetup(){
perc = new Percolation(5);
}
@AfterClass
public static void testCleanup(){
perc = null;
}
@Test(expected = IndexOutOfBoundsException.class)
public void testIndexErrorThrown(){
perc.open(0,5);
}
}
Compilation was accomplished with the following commands. Both .java files are in the same folder, so too is a directory titled algs4.jar with the algs4.jar fully expanded such that each class exists as its own file. Therefore in the working directory there are the following files.
Percolation.java
test_Percolation.java
algs4/WeightedQuickUnionUF.class
I feel that this is an important point because even though algs4.jar is within the classpath, the Percolation.java file ONLY compiles when I have algs4/WeightedQuickUnionUF.class in the current working directory. For some reason I cannot get the import to work with the .jar in the classpath.
>javac Percolation.java
>javac test_Percolation.java
Please note that in Percolation.java if I changed the import line from import algs4.*;
to import algs4.WeightedQuickUnionUF;
I receive the following error at compile time.
Percolation.java:23: error: cannot access WeightedQuickUnionUF
import algs4.WeightedQuickUnionUF;
^
bad class file: ./algs4/WeightedQuickUnionUF.class
class file contains wrong class: WeightedQuickUnionUF
Please remove or make sure it appears in the correct subdirectory of the classpath.
Having compiled both files successfully, I attempted to use the solutions found from the following question. Entering the command java org.junit.runner.JUnitCore test_Percolation
or the more verbose but seemingly unecessary command java -cp /Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore test_Percolation
both yield the following error.
JUnit version 4.11
Could not find class: test_Percolation
Time: 0.002
OK (0 tests)
I suspect that there is something wrong with my system configuration on top of the effects of my limited knowledge of Java and importing using Java. I know these questions have been posted elsewhere, but the solutions to said questions have not worked on my machine. Perhaps I have missed a nuance of the answers to other questions.
I am using Mac OSX 10.8.5 doing all of the programming and compilling from the command line using javac 1.7.0_07.
Look at your command-line:
java -cp /Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore
You've got the JUnit jar file on your class path - but not the current directory. So it won't be able to find any classes that aren't either "system" classes or in JUnit.
Try something like:
java -cp .:/Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore
Ideally you should also start using packages though - and using more conventional class names. (test_Percolation
violates normal conventions.)
See more on this question at Stackoverflow