Junit测试失败时在文件中收集JSON对象

问题描述 投票:0回答:1

我有〜50个JSON数组作为插入到单元测试中的模型数组,以比较结果配置。每个文件如下所示:0.json1.json ...等

[{{model1},{model2},{model3}]我正在尝试运行单元测试以比较生成的配置,并希望以这样的方式运行测试:测试本身保持运行并在断言失败时收集模型并将其输出到某个地方的json文件。

说,model2失败,我想将model2收集到文件output.json中作为数组

到目前为止,代码看起来像这样,即使测试是逐个文件进行,也可以,但是可以节省我很多时间:

@Test
public void compareAWithB() throws Exception {
    File lbJsonFile1 = new File("src/test/resources/iad_ad3/6.json");
        compareAWithBHelper(lbJsonFile1);
}

public void compareAWithBHelper(File lbJsonFile) throws Exception {
        Model[] dtos = new ObjectMapper().readValue(lbJsonFile, Model[].class);
        for(Model dto : dtos) {
            Model model = ModelConverter.apiToDao(dto);
            String A = doSomeThing();
            String B = doSomething2();

            Assert.assertEquals(A,B);
//Required: if assert fails, collect the json object and continue
}

[我尝试在AssertJ中使用SoftAssertions,但是很奇怪,它没有打印出所有json对象,或者,也许我不太了解checkThat()方法。

尝试使用collectors.checkThat,无法使其可靠运行。这是一个生产区域,因此,没有太多的错误余地,并且想要减少手动工作。

再次尝试使用收集器作为stackoverflow上的帖子之一,无法使其可靠地工作

/*try {
                collector.checkThat(A, CoreMatchers.equalTo(B));
            } catch (AssertionError error) {
                System.out.println(dto.toString());
                throw new AssertionError(error.getMessage());
}*/

有人可以帮忙吗?

json junit collectors assertj
1个回答
0
投票

如果您想收集所有断言错误而不是在第一个错误时停止,那么软断言是一个很好的选择。要开始使用软断言,您可以按照此处提供的指南进行操作:https://assertj.github.io/doc/#assertj-core-soft-assertions

collector.checkThat并非来自AssertJ(代码示例中都没有),这有点令人困惑,我建议编写可重复的测试,以便人们可以更轻松地提供帮助。

或者,如果您正在处理JSON,则可以尝试通过https://github.com/lukas-krecan/JsonUnit解决,它提供了一流的公民JSON断言。

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.