Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit5 @ParameterizedTest How to pass array as one of parameter

I have a test in which I want to pass three parameters:

  1. String
  2. Enum
  3. Array of Strings

Example:

@ParameterizedTest
    @CsvSource({
            "/path/to/first/file.xlsx, FIRST, {THIRD PARAMETER SHOULD BE ARRAY OF STRINGS}",
            "/path/to/second/file.xlsx, SECOND, {THIRD PARAMETER SHOULD BE ARRAY OF STRINGS}"})
    void uploadFile(String path, FileType type, String[] errors) {
        HttpEntity httpEntity = prepareFileUploadEntity(path, type);

        ResponseEntity<ArrayList> response = getRestTemplate(AppRole.USER).exchange(UPLOAD_URL, HttpMethod.POST, httpEntity, ArrayList.class);

        assertNotNull(response);
        assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
        assertEquals(errors.length, response.getBody().size());
        for (String error : errors) {
            assertTrue(response.getBody().contains(error));
        }
    }

How can I pass the third parameter as an array of strings, cause now I have the error that third parameter can`t be resolved:

org.junit.jupiter.api.extension.ParameterResolutionException: Error resolving parameter at index 2
like image 879
Volodymyr Nazarenko Avatar asked Oct 20 '17 12:10

Volodymyr Nazarenko


People also ask

How do you write parameterized test cases in junit5?

Writing Our First Parameterized TestsAdd a new test method to our test class and ensure that this method takes a String object as a method parameter. Configure the display name of the test method. Annotate the test method with the @ParameterizedTest annotation. This annotation identifies parameterized test methods.


1 Answers

@CsvSource uses implicit conversion to convert CSV values to primitives, Enums or Dates. For other types like Arrays, you need explicit conversion.

Assuming you have a CSV annotation in a format like @CsvSource("abc, 123, 'foo, bar'"), you can implement an argument converter like this to treat the last CSV column as an array:

import org.junit.jupiter.params.converter.ArgumentConversionException;
import org.junit.jupiter.params.converter.SimpleArgumentConverter;

public class StringArrayConverter extends SimpleArgumentConverter {

    @Override
    protected Object convert(Object source, Class<?> targetType) throws ArgumentConversionException {
        if (source instanceof String && String[].class.isAssignableFrom(targetType)) {
            return ((String) source).split("\\s*,\\s*");
        } else {
            throw new IllegalArgumentException("Conversion from " + source.getClass() + " to "
                                               + targetType + " not supported.");
        }
    }

}

Then you can use that converter on the third argument:

@ParameterizedTest
@CsvSource("abc, 123, 'foo, bar'")
void test(String column1, int column2, @ConvertWith(StringArrayConverter.class) String[] column3) {
    assertEquals(column1, "abc");
    assertEquals(column2, 123);
    assertEquals(column3[0], "foo");
    assertEquals(column3[1], "bar");
}
like image 108
kapex Avatar answered Oct 15 '22 01:10

kapex