Migrating from TestNG to JUnit Jupiter - junit-team/junit-framework GitHub Wiki

Migrating from TestNG to JUnit Jupiter

Quick Notes

  • Test classes should be annotated with @TestInstance(Lifecycle.PER_CLASS) to align with TestNG semantics.
  • @BeforeClass/@AfterClass methods should be annotated with @BeforeAll/@AfterAll.
  • Tests using data providers should be converted to parameterized tests (@ParameterizedTest).
  • Use @ParameterizedTest(autoCloseArguments=false) when types of the arguments implement AutoCloseable and JUnit should not close them.
  • The order of the expected and actual arguments to assertion methods such as assertEquals(...) needs to be swapped so that the expected result precedes the actual result.
  • Usage of expectThrows(...) should be changed to assertThrows(...).
  • Tests that threw SkipException should make use of the the Assumptions API.

Automated Migration Support

Fixes for Errors and Failures

You should apply following fixes when you encounter the error shown below.

Make argument source factory method static

Method 'public java.lang.Object[][] org.openjdk.tests.javac.FDTest.caseGenerator()'
  must be static: local factory methods must be static
  unless the PER_CLASS @testinstance lifecycle mode is used;
  external factory methods must always be static.

Make @AfterAll-annotated method static

[ERROR] @afterall method 'public void org.openjdk.tests.vm.FDSeparateCompilationTest.cleanupCompilerCache()'
  must be static unless the test class
  is annotated with `@TestInstance(Lifecycle.PER_CLASS)`.

Remove static modifier from @Test-annotated methods

This configuration error should have been detected by JUnit Jupiter and reported via the Discovery Issues API: https://docs.junit.org/current/user-guide/#running-tests-discovery-issues

Add Custom TestInstanceFactory

Class [FDTests] must declare a single constructor
Class [MethodReferenceTestKinds] must declare a single constructor

Example Migrations