Java 找不到符号 assertEquals
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20631621/
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
Cannot find symbol assertEquals
提问by Giome Pool Guy
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.
我正在尝试为计算器编写我的第一个单元测试,但 NetBeans 说它找不到符号assertEquals
和注释@Test
。
我应该包括一些东西吗?
我使用的是 NetBeans 7.3.1 和 W7。
package calculator;
import org.junit.Assert.*;
public class UnitTests{
@Test
public void checkAdd(){
assertEquals(2, Calculator.rpnCalc(" 2 3 + "));
}
}
EDIT: Thanks guys, importing it as static helped. Test annotation required only including
编辑:谢谢你们,将它作为静态导入有帮助。只需要测试注释,包括
import org.junit.Test;
导入 org.junit.Test;
采纳答案by bobbel
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:
assertEquals
是一个静态方法。由于您不能使用静态方法而不以静态方式显式导入它们,因此您必须使用:
import org.junit.Assert;
...
Assert.assertEquals(...)
or:
或者:
import static org.junit.Assert.assertEquals;
...
assertEquals(...)
For @Test
it's a little bit different. @Test
is an annotation as you can see by the @
. Annotations are imported like classes.
因为@Test
它有点不同。@Test
是一个注释,如您所见@
。注释像类一样导入。
So you should import it like:
所以你应该像这样导入它:
import org.junit.Test;
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?.
通常避免在导入时使用通配符,例如import org.junit.*
. 原因请参见为什么在 Java 导入语句中使用通配符不好?.
回答by Akshay Vijay Jain
I am working on JUnit in java 8 environment, using jUnit4.12
我正在使用 jUnit4.12 在 java 8 环境中处理 JUnit
for me: compiler was not able to find the method assertEquals, even when I usedimport org.junit.Assert;
对我来说:编译器无法找到方法 assertEquals,即使我使用import org.junit.Assert;
So I changed assertEquals("addb", string);
toAssert.assertEquals("addb", string);
所以我改变了assertEquals("addb", string);
对Assert.assertEquals("addb", string);
So if you are facing problem regarding assertEqual
not recognized, then change it to Assert.assertEquals(,);
it should solve your problem
因此,如果您遇到有关assertEqual
无法识别的问题,请将Assert.assertEquals(,);
其更改为它应该可以解决您的问题
回答by isapir
JUnit 5 Jupiter
JUnit 5木星
In JUnit 5 the package name has changed and the Assertions are at org.junit.jupiter.api.Assertions
and Assumptions at org.junit.jupiter.api.Assumptions
在 JUnit 5 中,包名发生了变化,断言在org.junit.jupiter.api.Assertions
,假设在org.junit.jupiter.api.Assumptions
So you have to add the following static import
:
所以你必须添加以下内容static import
:
import static org.junit.jupiter.api.Assertions.*;
See also http://junit.org/junit5/docs/current/user-guide/#writing-tests-assertions
另见http://junit.org/junit5/docs/current/user-guide/#writing-tests-assertions
回答by Tu Minh
You have to add the dependency to pom.xml file
您必须将依赖项添加到 pom.xml 文件
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
回答by Ali Tamoor
I was having the same problem cannot resolve symbol Assert
i have tried these solutions by adding the different import from the different answers.
我遇到了同样的问题,cannot resolve symbol Assert
我通过从不同的答案中添加不同的导入来尝试这些解决方案。
- import org.junit.Assert;
- import static org.junit.Assert.*;
- import static org.junit.Assert.assertEquals;
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.Assert;
- 导入 org.junit.Assert;
- 导入静态 org.junit.Assert.*;
- 导入静态 org.junit.Assert.assertEquals;
- 导入静态 org.junit.jupiter.api.Assertions.*;
- 导入 org.junit.Assert;
but the solution that did the magic was just place the junit-4.12.jar
in the app\lib
ditectory and just build the project, and import like this
但是神奇的解决方案只是将junit-4.12.jar
放在app\lib
字典中并构建项目,然后像这样导入
import org.junit.Assert;
you can download the junit-4.12.jar
from here
你可以junit-4.12.jar
从这里下载
回答by Paolo Secci
Using IntelliJ 2019.2.4 with a start.sping.io default setup...
使用 IntelliJ 2019.2.4 和 start.sping.io 默认设置...
import static org.junit.jupiter.api.Assertions.assertEquals;
but now instead of
但现在而不是
Assert.assertEquals(expected, actual);
use
用
assertEquals(expected, actual);