136

I bump version of junit to 4.11 and get:

[WARNING] [deprecation] Assert in junit.framework has been deprecated
[WARNING] [deprecation] Assert in junit.framework has been deprecated
....

How and to what migrate?

gavenkoa
  • 37,355
  • 13
  • 206
  • 248

6 Answers6

245

As it seems the Assert class has been moved from junit.framework to org.junit.Assert in JUnit 4.0 - you can use that instead, it's not deprecated.

rogerdpack
  • 50,731
  • 31
  • 212
  • 332
Alex Stockinger
  • 2,732
  • 1
  • 14
  • 15
81

Change your import statement from

import junit.framework.Assert;

to

import org.junit.Assert; 

and this will rectify your JUnit deprecation warnings.

dmeehan
  • 2,164
  • 2
  • 21
  • 28
5

After facing this problem I have tried lots of ways to solve this but failed again and again.

The good thing is: I have download junit-4.12.jar file from here and added the jar file in the project section under the libs folder. If previously any kind of Junit dependancy exist in the project then remove that from the build.gradle and build + clean your project.

It is worked for me. Hope it will work for you.

Note: Take a look in the image that I attached in below.

Thank you

enter image description here

Shahadat Hossain
  • 483
  • 4
  • 18
  • This solution worked for me, i just removed the dependency f`androidTestImplementation 'junit:junit:4.12'` from the `build.gradle(App level)` and place the `junit-4.12.jar` in the `app\libs` directory and `build` the project, thanks man, you saved me – Ali Tamoor Aug 29 '19 at 10:03
3

We had a large number of tests with many assertions.

Adding something like

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

to the import statements also helped to limit the changes in test code.

solleks
  • 89
  • 1
  • 4
3

Both are depricated:

junit.framework.Assert.assertThat
org.junit.Assert.assertThat

According to docs, use Instead:

org.hamcrest.MatcherAssert.assertThat
Salam El-Banna
  • 3,182
  • 1
  • 17
  • 30
  • But `hamcrest` is a separate project (and throws another exception to indicate failures). Can you clarify what is the purpose of https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/Assertions.html is JUnit 5? – gavenkoa Jan 16 '21 at 20:18
0

You can refer to jUnit4 Assert class methods from JUnit4

Sandeep
  • 41
  • 3