Warning: The method assertEquals from the type Assert is deprecated

JavaJunit

Java Problem Overview


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

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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

The third parameter is 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: https://stackoverflow.com/questions/5686755/meaning-of-epsilon-argument-of-assertequals-for-double-values

Solution 3 - Java

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

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

possible duplicate of differences between 2 JUnit Assert classes

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrad ParksView Question on Stackoverflow
Solution 1 - JavaStefan BirknerView Answer on Stackoverflow
Solution 2 - Javatommy.qichangView Answer on Stackoverflow
Solution 3 - JavaLanguoguangView Answer on Stackoverflow