【发布时间】:2014-06-04 18:39:40
【问题描述】:
与此主题的其他问题类似,我在让 JUnit 4.11 识别我的班级时遇到问题。
和here发现的问题类似,我正在使用algs4.jar写一个类,Percolation,都是在默认包里的。就像在我链接的帖子中一样,我也很难将 algs4 导入我的班级以使用其中找到的所需的 WeightedWuickUnionUF 班级。我终于能够使用以下设置编译程序。
> 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
渗透.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);
}
}
使用以下命令完成编译。两个 .java 文件都在同一个文件夹中,一个名为 algs4.jar 的目录也是如此,其中 algs4.jar 完全展开,这样每个类都作为自己的文件存在。因此在工作目录中有以下文件。
Percolation.java
test_Percolation.java
algs4/WeightedQuickUnionUF.class
我觉得这很重要,因为即使 algs4.jar 在类路径中,Percolation.java 文件也只有当我在当前工作目录中有 algs4/WeightedQuickUnionUF.class 时才会编译。由于某种原因,我无法让导入与类路径中的 .jar 一起使用。
>javac Percolation.java
>javac test_Percolation.java
请注意,在 Percolation.java 中,如果我将导入行从 import algs4.*; 更改为 import algs4.WeightedQuickUnionUF;,我会在编译时收到以下错误。
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.
成功编译这两个文件后,我尝试使用从以下question 中找到的解决方案。输入命令java org.junit.runner.JUnitCore test_Percolation 或更冗长但看似不必要的命令java -cp /Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore test_Percolation 都会产生以下错误。
JUnit version 4.11
Could not find class: test_Percolation
Time: 0.002
OK (0 tests)
除了我对 Java 的有限知识和使用 Java 导入的影响之外,我怀疑我的系统配置有问题。我知道这些问题已在其他地方发布,但上述问题的解决方案在我的机器上不起作用。也许我错过了其他问题答案的细微差别。
我正在使用 Mac OSX 10.8.5 使用 javac 1.7.0_07 从命令行执行所有编程和编译。
【问题讨论】:
标签: java junit jar import junit4