存储/记录/打印errorcollector收集的所有错误

时间:2014-06-23 05:42:47

标签: java junit errorcollector

我一直在使用ErrorCollector规则来收集失败的测试用例,代码看起来像这样:

@Rule
ErrorCollector collector = new ErrorCollector();

Map<X, Y> inputOutputMap = new HashMap<X,Y>();
inputOutputMap.add(new X("Input"), new Y("Output"));

//Mocking service layers and returning expected outputs.

for(Entry<X,Y> entry : inputOutputMap.entrySet()){

     String url = "/do/something"        

     Gson gson = new Gson();
     String json = gson.toJson();

    try{
    this.mockMvc.perform(get(url)
                .contentType(MediaType.APPLICATION_JSON)
                .content(json)).andExpect(status().isInternalServerError());
    } catch(Exception e) {
        collector.checkThat(e.getMessage, entry.getValue().getMessage());
    } 

}

所以我的问题是在收集所有错误之后是否有一种方法我可以打印所有错误,我编写测试用例的方式是这种写方式 - 迭代映射并检查相应输入的输出?

1 个答案:

答案 0 :(得分:1)

您可以展开ErrorCollector并公开verify。然后调用它并捕获MultipleFailureException。从那里获得Throwables通过getFailures

getFailures

这可能比从头开始重新实施ErrorCollector要好。