Class ParamsProviderUtil


  • public class ParamsProviderUtil
    extends Object
    Utility class for building argument stream for MethodSource.
    • Method Detail

      • buildArgumentDimensions

        public static Stream<org.junit.jupiter.params.provider.Arguments> buildArgumentDimensions​(org.junit.jupiter.params.provider.Arguments... dimensions)
        This is a utility to be used with junit-jupiter-params argument expander. It will instead of just returning the argument stream provided, will take a set of arguments, each with a specific type, and return the cross product of these argument lists.

        E.g. providing two dimensions, content type and locale, e.g. like this:

        
         public class MyTest {
             public static Stream&lt;Arguments&gt; myParamsSource() {
                 return buildArgumentDimensions(
                     arguments(ContentType.WALLPAPER, ContentType.AUDIO),
                     arguments(Locale.US, Locale.DE, Locale.SIMPLIFIED_CHINESE));
             }
             {@literal@}ParameterizedTest
             {@literal@}MethodSource("myParamsSource")
             public void testMyParams(ContentType contentType, Locale locale) {
                 // The test.
             }
         }
         

        Will create 6 argument arrays, one for each combination of content type and locale as if the input was this:

        
         return Stream.of(
             arguments(ContentType.WALLPAPER, Locale.US),
             arguments(ContentType.WALLPAPER, Locale.DE),
             arguments(ContentType.WALLPAPER, Locale.SIMPLIFIED_CHINESE),
             arguments(ContentType.AUDIO, Locale.US),
             arguments(ContentType.AUDIO, Locale.DE),
             arguments(ContentType.AUDIO, Locale.SIMPLIFIED_CHINESE),
         );
         }

        This can significantly shorten the argument list especially with larger set of dimensions and longer lists of options for each. It accepts null values and does not accept empty dimensions. Each dimension is called a 'layer' in the input.

        Parameters:
        dimensions - The available dimensions.
        Returns:
        The argument arrays.