[英]Error with import of org.junit.Assert
I'm having a little problem with a unit-test my professor gave me. 我的教授给了我一个单元测试的问题。 Upon compilation, I recieve the following errors:
编译后,我收到以下错误:
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;
I have downloaded JUnit and I can compile a similar file, so why am I having problems with this? 我已经下载了JUnit,我可以编译一个类似的文件,为什么我有这个问题呢? The code is:
代码是:
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());
}
}
Assuming that you have the JUnit dependency in the classpath, use import static
for the assert methods: 假设您在类路径中具有JUnit依赖关系 ,请对assert方法使用
import static
:
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Or simply use: 或者只是使用:
import static org.junit.Assert.*;
You should add the keyword static
to import it. 您应该添加关键字
static
以导入它。 An example: 一个例子:
import static org.junit.Assert.assertFalse;
What you are looking for is a Static import 您正在寻找的是静态导入
The line import org.junit.Assert.assertArrayEquals;
行
import org.junit.Assert.assertArrayEquals;
is referencing the method assertArrayEquals
from the class org.junit.Assert
从类
org.junit.Assert
引用方法assertArrayEquals
Importing a static method so that it is callable like assertEquals(0,orderedArray.size());
导入一个静态方法,使其可以像
assertEquals(0,orderedArray.size());
一样调用assertEquals(0,orderedArray.size());
is done with a static import line. 使用静态导入行完成。 Try out the following:
试试以下内容:
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Alternatively you can: 或者你可以:
import static org.junit.Assert.*;
, or you could: ,或者你可以:
import org.junit.Assert;
and reference the methods like 并参考像这样的方法
Assert.assertEquals(0,orderedArray.size());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.