【发布时间】:2017-03-23 16:03:23
【问题描述】:
public Map<String, List<Tuple4>> buildTestcases(ArrayList<Tuple4> list){
Map<String, List<Tuple4>> map = new LinkedHashMap<String, List<Tuple4>>();
for(Tuple4 element: list){
String[] token = element.c.split("\\s");
if (!map.containsKey(token[1])) {
map.put(token[1], new ArrayList<Tuple4>());
}
map.get(token[1]).add(element);
}
System.out.println(map);
return map;
}
public Tuple4(String a, String b, String c, String d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
我正在为某些匹配的测试用例构建一个测试套件。现在我想将它转换为一个数组,因为我正在从中构造一个 dynamicTest:
return Stream.of(<Array needed>).map(
tuple -> DynamicTest.dynamicTest("Testcase: ", () -> { ... }
有没有办法将它转换为像Object[String][Tuple4]这样的数组
编辑:
好的,现在我有了这段代码:
`@TestFactory 公共流 dynamicTuple4TestsFromStream() 抛出 IOException{ 初始化();
return map.entrySet().stream()
.flatMap(entry ->
entry.getValue().stream()
.map(s -> new AbstractMap.SimpleEntry<>(entry.getKey(), s)))
.forEach(e -> DynamicTest.dynamicTest("Testcase: " +e.getKey(), () -> {
tester = new XQueryTester(e.getValue().a, e.getValue().b);
if(e.getValue().c.contains("PAY")){
Assert.assertTrue(tester.testBody(e.getValue().c,e.getValue().d));
}
})); }`
我得到了这个异常:
incompatible types: void cannot be converted to java.util.stream.Stream<org.junit.jupiter.api.DynamicTest>
如何/为什么?
【问题讨论】:
-
Object[String][Tuple4]? -- 我不认为这是 Java 数组的规范。 Java 数组由整数索引。 -
是的,抱歉我没说清楚,我的意思是在第一个索引中存储了字符串,在第二个索引中存储了我的元组。
-
forEach()采用Consumer<?>-- 这是一个带有一个返回 void 的参数的函数。你给它传递了一个Function<Entry,<Stream<DynamicTest>>,这就是它抱怨的原因。您可以通过编写更小的函数并将它们分配给变量来使这段代码更易于阅读,就像使用“extract-method”使方法更简单一样。
标签: java arrays hashmap toarray