java 导入 org.junit.Assert 时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43305739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error with import of org.junit.Assert
提问by Leo
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;
我的教授给我的单元测试有点问题。编译后,我收到以下错误: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? The code is:
我已经下载了 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());
}
}
回答by cassiomolin
Assuming that you have the JUnit dependencyin the classpath, use import static
for the assert methods:
假设您在类路径中有JUnit 依赖项,请使用import static
assert 方法:
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.*;
回答by 7H3_H4CK3R
What you are looking for is a Static import
您正在寻找的是静态导入
The line import org.junit.Assert.assertArrayEquals;
is referencing the method assertArrayEquals
from the class org.junit.Assert
该行import org.junit.Assert.assertArrayEquals;
正在引用assertArrayEquals
类中的方法org.junit.Assert
Importing a static method so that it is callable like assertEquals(0,orderedArray.size());
is done with a static import line. Try out the following:
导入静态方法以使其可调用,就像assertEquals(0,orderedArray.size());
使用静态导入行一样。尝试以下操作:
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());
回答by Pau
You should add the keyword static
to import it. An example:
您应该添加关键字static
来导入它。一个例子:
import static org.junit.Assert.assertFalse;