Junit5 @ParameterizedTest 如何将数组作为参数之一传递
Junit5 @ParameterizedTest How to pass array as one of parameter
我有一个测试,我想在其中传递三个参数:
- 字符串
- 枚举
- 字符串数组
示例:
@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));
}
}
如何将第三个参数作为字符串数组传递,因为现在出现无法解析第三个参数的错误:
org.junit.jupiter.api.extension.ParameterResolutionException: Error resolving parameter at index 2
@CsvSource
使用 implicit conversion to convert CSV values to primitives, Enums or Dates. For other types like Arrays, you need explicit conversion.
假设您有一个格式如 @CsvSource("abc, 123, 'foo, bar'")
的 CSV 注释,您可以实现这样的参数转换器以将最后一个 CSV 列视为数组:
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.");
}
}
}
然后你可以在第三个参数上使用那个转换器:
@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");
}
小修正,在
return ((String) source).split("\s*,\s*");
应该是不同的符号(例如“;”)而不是“,”
那么在测试中应该是
@ParameterizedTest
@CsvSource("abc, 123, foo; bar")
适合我的最终版本:
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.");
}
}
}
测试:
@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");
}
我有一个测试,我想在其中传递三个参数:
- 字符串
- 枚举
- 字符串数组
示例:
@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));
}
}
如何将第三个参数作为字符串数组传递,因为现在出现无法解析第三个参数的错误:
org.junit.jupiter.api.extension.ParameterResolutionException: Error resolving parameter at index 2
@CsvSource
使用 implicit conversion to convert CSV values to primitives, Enums or Dates. For other types like Arrays, you need explicit conversion.
假设您有一个格式如 @CsvSource("abc, 123, 'foo, bar'")
的 CSV 注释,您可以实现这样的参数转换器以将最后一个 CSV 列视为数组:
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.");
}
}
}
然后你可以在第三个参数上使用那个转换器:
@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");
}
小修正,在
return ((String) source).split("\s*,\s*");
应该是不同的符号(例如“;”)而不是“,”
那么在测试中应该是
@ParameterizedTest
@CsvSource("abc, 123, foo; bar")
适合我的最终版本:
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.");
}
}
}
测试:
@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");
}