-
-
Notifications
You must be signed in to change notification settings - 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
/@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.
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