Java 警告:不推荐使用 Assert 类型的方法 assertEquals

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22541455/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:22:29  来源:igfitidea点击:

Warning: The method assertEquals from the type Assert is deprecated

javajunit

提问by Brad Parks

Since the method Assert.assertEqualsis deprecated, which method are we supposed to use now?

由于该方法Assert.assertEquals已被弃用,我们现在应该使用哪种方法?

The following code:

以下代码:

String arg1 = "test";
String arg2 = "me";

Assert.assertEquals(arg1, arg2);

Gives the following warnings:

给出以下警告:

Multiple markers at this line

  • The method assertEquals(String, String) from the type Assert is deprecated
  • The type Assert is deprecated

此行有多个标记

  • 不推荐使用 Assert 类型的 assertEquals(String, String) 方法
  • 不推荐使用类型断言

采纳答案by Stefan Birkner

You're using junit.framework.Assertinstead of org.junit.Assert.

您正在使用junit.framework.Assert而不是org.junit.Assert.

回答by tommy.qichang

this method also encounter a deprecate warning:

此方法也遇到弃用警告:

org.junit.Assert.assertEquals(float expected,float actual) //deprecated

It is because currently junit prefer a third parameter rather than just two float variables input.

这是因为当前 junit 更喜欢第三个参数,而不仅仅是两个浮点变量输入。

The third parameter is delta:

第三个参数是delta:

public static void assertEquals(double expected,double actual,double delta) //replacement

this is mostly used to deal with inaccurate Floating point calculations

这主要用于处理不准确的浮点计算

for more information, please refer this problem: Meaning of epsilon argument of assertEquals for double values

有关更多信息,请参阅此问题:双值的 assertEquals 的 epsilon 参数的含义

回答by Languoguang

When I use Junit4, import junit.framework.Assert; import junit.framework.TestCase; the warning info is :The type of Assert is deprecated

当我使用 Junit4 时,导入 junit.framework.Assert; 导入 junit.framework.TestCase; 警告信息是:不推荐使用断言的类型

when import like this: import org.junit.Assert; import org.junit.Test; the warning has disappeared

像这样导入时: import org.junit.Assert; 导入 org.junit.Test; 警告消失了

possible duplicate of differences between 2 JUnit Assert classes

2 个 JUnit 断言类之间差异可能重复