导入组织时出现java错误。朱尼特。明确肯定
我对教授给我的单元测试有点问题。编译时,我收到以下错误:
cannot find symbol import org.junit.Assert.assertArrayEquals;
cannot find symbol import org.junit.Assert.assertEquals;
import org.junit.Assert.assertFalse;
import org.junit.Assert.assertTrue;
我已经下载了JUnit,我可以编译一个类似的文件,为什么我在这方面有问题呢? 代码是:
import java.util.Comparator;
import org.junit.Assert.assertArrayEquals;
import org.junit.Assert.assertEquals;
import org.junit.Assert.assertFalse;
import org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
public class SortingTests {
class IntegerComparator implements Comparator<Integer> {
@Override
public int compare(Integer i1, Integer i2) {
return i1.compareTo(i2);
}
}
private Integer i1,i2,i3;
private OrderedArray<Integer> orderedArray;
@Before
public void createOrderedArray(){
i1 = -12;
i2 = 0;
i3 = 4;
orderedArray = new OrderedArray<>(new IntegerComparator());
}
@Test
public void testIsEmpty_zeroEl(){
assertTrue(orderedArray.isEmpty());
}
@Test
public void testIsEmpty_oneEl() throws Exception{
orderedArray.add(i1);
assertFalse(orderedArray.isEmpty());
}
@Test
public void testSize_zeroEl() throws Exception{
assertEquals(0,orderedArray.size());
}
}
# 1 楼答案
假设类路径中有JUnit dependency,那么对assert方法使用
import static
:或者简单地使用:
# 2 楼答案
您应该添加关键字
static
来导入它。举个例子:# 3 楼答案
你要找的是一个静态导入
行
import org.junit.Assert.assertArrayEquals;
引用了类org.junit.Assert
中的方法assertArrayEquals
通过静态导入行导入静态方法,使其像
assertEquals(0,orderedArray.size());
一样可调用。尝试以下方法:或者你可以:
,或者你可以:
并参考以下方法