186

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
Community
  • 1
  • 1
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
  • 4
    possible duplicate of [differences between 2 JUnit Assert classes](http://stackoverflow.com/questions/291003/differences-between-2-junit-assert-classes) – Joe Mar 25 '14 at 12:32

3 Answers3

356

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

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
  • 18
    Thanks.... i realized that later after i posted this.... I still think it's worth including here as a question/answer, as it may pop up for others, and now they'll find this when they google it. – Brad Parks Mar 20 '14 at 22:08
  • 3
    [This answer](http://stackoverflow.com/a/291045/411282) to the question linked above provides some additional context. – Joshua Goldberg Apr 27 '15 at 17:56
  • 4
    To future people who also found this useful, please don't comment a thank you - just upvote the question and answer. http://meta.stackexchange.com/questions/126180/is-it-acceptable-to-write-a-thank-you-in-a-comment it's not a big deal, but we don't want this answer getting cluttered. – Jeutnarg Oct 20 '16 at 20:53
  • 1
    I am pretty sure this is also deprecated in org.junit.Assert too. I think as is commented on by Tommy.qichang below, it requires an additional parameter for delta (level of precision within which is considered correct). – Andrew S Aug 31 '21 at 18:22
  • 1
    This question is about comparing Strings and not floats. For Strings it is not deprecated. – Stefan Birkner Sep 02 '21 at 07:06
56

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: Meaning of epsilon argument of assertEquals for double values

Xanlantos
  • 887
  • 9
  • 18
tommy.qichang
  • 670
  • 5
  • 5
  • Delta is the value that the 2 numbers can be off by. So it will assert to true as long as Math.abs(expected - actual) < delta. Use 0 if you want 100% accuracy. – Janac Meena Feb 10 '21 at 18:53
0

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

Languoguang
  • 2,166
  • 2
  • 10
  • 15