我正在寻找如何将参数传递到使用 Junit5 中的 @MethodSource 注释的方法中
例如,我需要调用 @MethodSource 注释并将值传递到方法“MyFactoryMethod”中。有可能的?有些像这样:
@ParameterizedTest
@MethodSource("MyFactoryMethod(10)")
void testWithMethodSource(int argument) {
assertNotEquals(9, argument);
}
static IntStream MyFactoryMethod(String var) {
return IntStream.range(0, var);
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
我在 Junit 测试用例上使用 @MethodSource 注释,以便从另一个方法接收 Map<String, Object>。
似乎 @MethodSource 无法支持“Map”对象。
这是我收到的错误:org.junit.platform.commons.PreconditionViolationException:无法将 java.util.HashMap 的实例转换为流:{1=Obj1, 2=Obj2}
您知道是否有一种方法可以像本例中那样接收“Map”对象?
@ParameterizedTest
@MethodSource("hashMapProvider")
void testMyMapObj(Map<String, Object> argument) {
assertNotNull(argument);
Object obj1 = argument.get("1");
}
static Map<String, Object> hashMapProvider() {
Map<String, Object> map = new HashMap<>();
map.put("1", "Obj1");
map.put("2", "Obj2");
return map;
}
Run Code Online (Sandbox Code Playgroud)