-
Notifications
You must be signed in to change notification settings - Fork 157
Migration from JUnit4 to JUnit Jupiter
Since: v2.0
All examples can be found as integration / acceptance tests in
junit-jupiters package
com.tngtech.test.junit.dataprovider.
-
Replace test dependency
org.junit:junitwithorg.junit.jupiter:junit-jupiter-engine- Hint: Currently thinking about only using
org.junit.jupiter:junit-jupiter-apiwith lack ofParameterResolversupport for dataprovider methods - Hint2: see also JUnit5 Migration Tips
- Hint: Currently thinking about only using
-
Replace test dependency
com.tngtech.java:junit-dataproviderorcom.tngtech.junit.dataprovider:junit4-dataproviderwithcom.tngtech.junit.dataprovider:junit-jupiter-dataprovider
-
JUnit dataprovider annotations are moved from
com.tngtech.java.junit.dataprovidertocom.tngtech.junit.dataprovider -
Replace
@RunWith(DataProviderRunner.class)with-
@ExtendWith(UseDataProviderExtension.class)for using@UseDataProviderand dataprovider methods or -
@ExtendWith(DataProviderExtension.class)for using@DataProviderand@DataProvider#value()to provide test data
-
-
Replace
@Testwith@TestTemplate- Eclipse search replace if
@Testis before dataprovider annotation:@Test(\s*)@(Use)?DataProvider-->@TestTemplate$1@$2DataProvider
- Eclipse search replace if
-
Replace usage of
FrameworkMethodin a dataprovider method withTestInfo-
TestInfo#getTestMethod()will provide anOptional<Method>which always contains a test method using it with JUnit dataprovider - Note: within a dataprovider method one can new use every parameter resolvable by Parameter Resolution
-
-
One might organize imports for all changed tests files
-
Optional: Replace format placeholder
%pwith%a- Note:
%pis only deprecated but not removed yet
- Note:
-
Complete resolvers code is completely refactored and resides in core package
com.tngtech.junit.dataprovider.resolvernow. -
ResolverStrategywas moved to separate class -
Usage of JUnit4s
FrameworkMethodwas replaced with JavasMethod -
Usage of JUnit4s
TestClasswas replaced withClass<?> -
Note:
MethodandClass<?>have a lot less features, compared toFrameworkMethodandTestClassbut you can use JUnit JupitersAnnotationSupportand / orReflectionSupportinstead
- Placeholders reside in core package
com.tngtech.junit.dataprovider.placeholdernow. -
Placeholdersclass to customize used placeholders is removed in favor of creating custom (meta) annotations- An example can be found here
-
ParameterPlaceholderis renamed toArgumentPlaceholderbecause it handles arguments instead of parameters- Method
formatAll(Object[])is changed toformatAll(List<Object>)
- Method
- A custom
DataConvertercan be used by creating a custom (meta) annotation- As a starting point you can use
CustomConverterDataProviderExtensionfrom here and use constructorDataProviderInvocationContextProvider(Class, DataConverter)orUseDataProviderInvocationContextProvider(Class, Class, DataConverter)with your customDataConverterinstead
- As a starting point you can use
- A custom
*Convertercan be provided- via
@DataProviderannotation, - by creating a custom (meta) annotation, or
- as default in a custom extension.
- via
- Examples and starting points can be found here.
-
ObjectArrayConverteris moved to core packagecom.tngtech.junit.dataprovider.convert
-
SingleArgConverteris moved to core packagecom.tngtech.junit.dataprovider.convert
-
StringConverteris moved to core packagecom.tngtech.junit.dataprovider.convertand refactored-
@DataProviderparameters are replaced withConverterContextto be independent from a concrete implementation
-
-
Removed
TestGeneratorand put the logic toAbstractDataProviderInvocationContextProviderandUseDataProviderInvocationContextProvider-
protected TestGenerator#generateExplodedTestMethodsFor(...)--> no pendant available as JUnit Jupiter behaves completely different -
TestGenerator#explodeTestMethod(...)-->UseDataProviderInvocationContextProvider#invokeDataProviderMethodToRetrieveData(...) -
TestGenerator#explodeTestMethod(...)-->AbstractDataProviderInvocationContextProvider#convertData(...)
-
-
Removed
TestValidatorentirely to adapt to JUnit Jupiter behavior which does not validate test cases beforehand
- TODO is there more?
- JUnit dataprovider methods must no longer be
public(same as test methods in JUnit Jupiter) - JUnit dataprovider methods must no longer be
staticif and only if class is annotated with@TestInstance(Lifecycle.PER_CLASS)- Example: Non-static dataprovider acceptance test
- JUnit Jupiter documentation on test instance lifcycle
- JUnit dataprovider annotation can be used as meta-annotations to be able to create custom composed annotations
- Examples: meta acceptance test
- JUnit Jupter documentation
- Test methods used with JUnit dataprovider can now have additional test method parameters according to
ParameterResolverextensions, e.g.TestInfo- Note: This does not work for test methods having a varargs parameter
- JUnit Jupter documentation
- Default format of a dataprovider test has changed to
[%i: %a[0..-1]]without method (%m) prefix- Reason: JUnit Jupiter already provides grouping tests by test method by default
- Home
- Motivation, Distinction and FAQs
- Getting started
- Version compatibility
- Migration guides
-
Features
- Array syntax
- String syntax
- Iterable syntax
- Custom dataprovider method resolvers
- Change
@DataProviderlocation - Varargs support
@BeforeClasssupport (JUnit4 only)- Customize test method name
- Access
FrameworkMethodin@DP - Utility methods
- Convention over configuration
- Kotlin support
- OSGi compatible
MANIFEST.MF
- Tips and Tricks
- Release Notes
- Eclipse Template