找不到符号assertEquals [英] Cannot find symbol assertEquals
问题描述
我正在尝试为计算器编写第一个单元测试,但是NetBeans表示找不到符号assertEquals
和注释@Test
.
我应该包括一些东西吗?
我正在使用NetBeans 7.3.1和W7.
I'm trying to write my first unit tests for a calculator, but NetBeans says it can't find the symbol assertEquals
and annotation @Test
.
Should i include something?
I'm using NetBeans 7.3.1 and W7.
package calculator;
import org.junit.Assert.*;
public class UnitTests{
@Test
public void checkAdd(){
assertEquals(2, Calculator.rpnCalc(" 2 3 + "));
}
}
谢谢大家,将其导入为静态帮助. 测试注释仅需包含
Thanks guys, importing it as static helped. Test annotation required only including
导入org.junit.Test;
import org.junit.Test;
推荐答案
assertEquals
是静态方法.由于必须先以静态方式显式导入静态方法,才能使用静态方法,因此必须使用以下两种方法之一:
assertEquals
is a static method. Since you can't use static methods without importing them explicitly in a static way, you have to use either:
import org.junit.Assert;
...
Assert.assertEquals(...)
或:
import static org.junit.Assert.assertEquals;
...
assertEquals(...)
对于@Test
,这有点不同.正如您在@
中看到的那样,@Test
是注释.注释就像类一样导入.
For @Test
it's a little bit different. @Test
is an annotation as you can see by the @
. Annotations are imported like classes.
因此,您应该像这样导入它:
So you should import it like:
import org.junit.Test;
通常避免在import org.junit.*
之类的导入上使用通配符.出于某些原因,请参见为什么要使用野生符号Java导入语句不好的智能卡?.
Generally avoid using wildcards on imports like import org.junit.*
. For reasons see Why is using a wild card with a Java import statement bad?.
这篇关于找不到符号assertEquals的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!