教授给我的单元测试出了点小问题。编译时,我收到了以下错误: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());
}
}
4条答案
按热度按时间nlejzf6q1#
假设类路径中有JUnit dependency,请对assert方法使用
import static
:或者简单地用途:
c2e8gylq2#
您要查找的是 * 静态导入 *
行
import org.junit.Assert.assertArrayEquals;
正在引用org.junit.Assert
类中的assertArrayEquals
方法导入一个静态方法,使其像
assertEquals(0,orderedArray.size());
一样可调用,这是通过静态导入行完成的。请尝试以下操作:或者,您可以:
,或者您可以:
并引用以下方法
kuarbcqp3#
您应该添加关键字
static
来导入它。示例:olqngx594#
如果您使用的是junit版本5或更高版本,那么请使用org.junit.jupiter.api.assertions。
路径已在junit 5中移动