- java.lang.Object
-
- net.morimekta.testing.junit4.DataProviderUtil
-
public class DataProviderUtil extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Object[][]
buildDataDimensions(List<?>... dimensions)
This is a utility to be used withjunit-dataprovider
.static Object[][]
makeDataParams(List<List<Object>> lists)
Convenience method to be able to use lists instead of arrays to build the dimensions.
-
-
-
Method Detail
-
buildDataDimensions
public static Object[][] buildDataDimensions(List<?>... dimensions)
This is a utility to be used withjunit-dataprovider
. It will instead of taking in an array of argument arrays, will get an array of available dimensions, each with possible values. It will then multiply all the possible dimension possibilities with each other and result the array of parameter array thatjunit-dataprovider
wants.E.g. providing two dimensions, content type and locale, e.g. like this:
{@literal@}RunWith(DataProviderRunner.class) public class MyTest { {@literal@}DataProvider public static Object[][] testParams() { return buildDataDimensions( List.of(ContentType.WALLPAPER, ContentType.AUDIO), List.of(Locale.US, Locale.DE, Locale.SIMPLIFIED_CHINESE)); } {@literal@}Test {@literal@}UseDataProvider("testParams") 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 new Object[][]{ {ContentType.WALLPAPER, Locale.US}, {ContentType.WALLPAPER, Locale.DE}, {ContentType.WALLPAPER, Locale.SIMPLIFIED_CHINESE}, {ContentType.AUDIO, Locale.US}, {ContentType.AUDIO, Locale.DE}, {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.
-
makeDataParams
public static Object[][] makeDataParams(List<List<Object>> lists)
Convenience method to be able to use lists instead of arrays to build the dimensions. This must be a a list of lists of equal sizes, with compatible argument objects in each list.- Parameters:
lists
- List of list of arguments.- Returns:
- Array of array of arguments.
-
-