Skip to content

Migration from JUnit4 to JUnit Jupiter Parameterized Tests

Andreas Schmid edited this page Nov 11, 2017 · 2 revisions

JUnit Jupiters @ParameterizedTest

Since: v2.0

Examples

All examples can be found as integration / acceptance tests in junit-jupiter-params's package com.tngtech.test.junit.dataprovider.

Dependencies

  • Replace test dependency org.junit:junit with org.junit.jupiter:junit-jupiter-params and org.junit.jupiter:junit-jupiter-engine

    • Hint: Currently thinking about only using org.junit.jupiter:junit-jupiter-api with lack of ParameterResolver support for dataprovider methods
    • Hint2: see also JUnit5 Migration Tips
  • Replace test dependency com.tngtech.java:junit-dataprovider or com.tngtech.junit.dataprovider:junit4-dataprovider with com.tngtech.junit.dataprovider:junit-jupiter-params-dataprovider

Test code

  • General: JUnit dataprovider annotations are renamed and moved from com.tngtech.java.junit.dataprovider to com.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
  • Replace usage of FrameworkMethod in a dataprovider method with TestInfo

    • TestInfo#getTestMethod() will provide an Optional<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

Framework code

Custom resolver

  • 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 Javas Method

  • Usage of JUnit4s TestClass was replaced with Class<?>

  • Note: Method and Class<?> have a lot less features, compared to FrameworkMethod and TestClass but you can use JUnit Jupiters AnnotationSupport and / or ReflectionSupport instead

Custom placeholder

  • 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

Custom converter

Custom DataConverter
  • A custom DataConverter can be used by creating a custom (meta) annotation
    • As a starting point you can use CustomConverterDataProviderArgumentProvider from here and use constructor AbstractStringDataProviderArgumentProvider(DataConverter) or AbstractUseDataProviderArgumentProvider(Class, DataConverter) with your custom DataConverter instead
Custom *Converter
  • A custom *Converter can be provided
    • via @DataProvider annotation,
    • by creating a custom (meta) annotation, or
    • as default in a custom extension.
  • Examples and starting points can be found here.
Custom ObjectArrayConverter
Custom SingleArgConverter
Custom StringConverter

Custom TestGenerator and TestValidator

  • Removed TestGenerator and put the logic to AbstractDataProviderArgumentProvider and AbstractUseDataProviderArgumentProvider

    • 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

New, missing features and behavioral changes

  • TODO is there more?

New features

  • 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)
  • JUnit dataprovider annotation can be used as meta-annotations to be able to create custom composed annotations
  • 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

Missing features

  • 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

Behavioral changes