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 implementAutoCloseable
and JUnit should not close them. - The order of the
expected
andactual
arguments to assertion methods such asassertEquals(...)
needs to be swapped so that theexpected
result precedes theactual
result. - Usage of
expectThrows(...)
should be changed toassertThrows(...)
. - Tests that threw
SkipException
should make use of the theAssumptions
API.
Automated Migration Support
Fixes for Errors and Failures
You should apply following fixes when you encounter the error shown below.
static
Make argument source factory method 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.
@AfterAll
-annotated method static
Make [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)`.
static
modifier from @Test
-annotated methods
Remove 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
TestInstanceFactory
Add Custom Class [FDTests] must declare a single constructor
Class [MethodReferenceTestKinds] must declare a single constructor