-
Notifications
You must be signed in to change notification settings - Fork 164
Migration from JUnit4 to JUnit Jupiter Parameterized Tests
Since: v2.0
All examples can be found as integration / acceptance tests in junit-jupiter-params's package com.tngtech.test.junit.dataprovider
.
-
Replace test dependency
org.junit:junit
withorg.junit.jupiter:junit-jupiter-params
andorg.junit.jupiter:junit-jupiter-engine
- Hint: Currently thinking about only using
org.junit.jupiter:junit-jupiter-api
with lack ofParameterResolver
support for dataprovider methods - Hint2: see also JUnit5 Migration Tips
- Hint: Currently thinking about only using
-
Replace test dependency
com.tngtech.java:junit-dataprovider
orcom.tngtech.junit.dataprovider:junit4-dataprovider
withcom.tngtech.junit.dataprovider:junit-jupiter-params-dataprovider
-
General: JUnit dataprovider annotations are renamed and moved from
com.tngtech.java.junit.dataprovider
tocom.tngtech.junit.dataprovider
-
Remove
@RunWith(DataProviderRunner.class)
entirely -
Replace
@Test
with@ParameterizedTest
- Eclipse search replace if
@Test
is before dataprovider annotation:@Test(\s*)@(Use)?DataProvider
-->@ParameterizedTest$1@$2DataProvider
- Eclipse search replace if
-
Replace usage of
FrameworkMethod
in 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
%p
with%a
- Note:
%p
is only deprecated but not removed yet
- Note:
-
Complete resolvers code is completely refactored and resides in core package
com.tngtech.junit.dataprovider.resolver
now. -
ResolverStrategy
was moved to separate class -
Usage of JUnit4s
FrameworkMethod
was replaced with JavasMethod
-
Usage of JUnit4s
TestClass
was replaced withClass<?>
-
Note:
Method
andClass<?>
have a lot less features, compared toFrameworkMethod
andTestClass
but you can use JUnit JupitersAnnotationSupport
and / orReflectionSupport
instead
- Customizing the formatter is (currently) not supported via JUnit Jupiter Parameterized Tests (as there is no
(easy) way to provide a custom display name formatter to
ParameterizedTestExtension
) - Instead you can use
@ParameterizedTest#name()
to at least minimally customize the display name
- A custom
DataConverter
can be used by creating a custom (meta) annotation- As a starting point you can use
CustomConverterDataProviderArgumentProvider
from here and use constructorAbstractStringDataProviderArgumentProvider(DataConverter)
orAbstractUseDataProviderArgumentProvider(Class, DataConverter)
with your customDataConverter
instead
- As a starting point you can use
- A custom
*Converter
can be provided- via
@DataProvider
annotation, - by creating a custom (meta) annotation, or
- as default in a custom extension.
- via
- Examples and starting points can be found here.
-
ObjectArrayConverter
is moved to core packagecom.tngtech.junit.dataprovider.convert
-
SingleArgConverter
is moved to core packagecom.tngtech.junit.dataprovider.convert
-
StringConverter
is moved to core packagecom.tngtech.junit.dataprovider.convert
and refactored-
@DataProvider
parameters are replaced withConverterContext
to be independent from a concrete implementation
-
-
Removed
TestGenerator
and put the logic toAbstractDataProviderArgumentProvider
andAbstractUseDataProviderArgumentProvider
-
protected TestGenerator#generateExplodedTestMethodsFor(...)
--> no pendant available as [JUnit Jupiter Parameterized Test][] behaves completely different -
TestGenerator#explodeTestMethod(...)
-->AbstractUseDataProviderArgumentProvider#invokeDataProviderMethodToRetrieveData(...)
-
TestGenerator#explodeTestMethod(...)
-->AbstractDataProviderArgumentProvider#convertData(...)
-
-
Removed
TestValidator
entirely to adapt to JUnit Jupiter Parameterized Tests 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
static
if 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
ParameterResolver
extensions, e.g.TestInfo
- Note: This does not work for test methods having a varargs parameter
- JUnit Jupter documentation
- Customizing the formatter is (currently) not supported in JUnit Jupiter Parameterized Tests and therefore custom
placeholders cannot be used anymore
-
@DataProvider#format()
was therefore removed
-
- Default format of a dataprovider test has changed to
[{index}] {arguments}
, see@ParameterizedTest#name()
- Home
- Motivation, Distinction and FAQs
- Getting started
- Version compatibility
- Migration guides
-
Features
- Array syntax
- String syntax
- Iterable syntax
- Custom dataprovider method resolvers
- Change
@DataProvider
location - Varargs support
@BeforeClass
support (JUnit4 only)- Customize test method name
- Access
FrameworkMethod
in@DP
- Utility methods
- Convention over configuration
- Kotlin support
- OSGi compatible
MANIFEST.MF
- Tips and Tricks
- Release Notes
- Eclipse Template