-
-
Couldn't load subscription status.
- Fork 1.6k
Migrating from TestNG to JUnit Jupiter
Christian Stein edited this page Sep 3, 2025
·
9 revisions
- Test classes should be annotated with
@TestInstance(Lifecycle.PER_CLASS)to align with TestNG semantics. -
@BeforeClass/@AfterClassmethods 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 implementAutoCloseableand JUnit should not close them. - The order of the
expectedandactualarguments to assertion methods such asassertEquals(...)needs to be swapped so that theexpectedresult precedes theactualresult. - Usage of
expectThrows(...)should be changed toassertThrows(...). - Tests that threw
SkipExceptionshould make use of the theAssumptionsAPI.
You should apply following fixes when you encounter the error shown below.
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.
[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)`.
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
Class [FDTests] must declare a single constructor
Class [MethodReferenceTestKinds] must declare a single constructor