0

I have ~50 JSON arrays as an array of models being plugged into Unit tests to compare resultant configs. Each file looks like this: 0.json 1.json... and so on

[{model1},{model2},{model3}] I am trying to run unit tests to compare the resultant configs and want to run the tests in a manner that the test itself keeps running and collect the models if an assertion fails and output it to a json file somewhere.

Say, model2 fails, I want to collect model2 into a file output.json as an array

Till now, the code looks like this, even if the test is file by file, its fine, but will save me days of effort:

@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
}

I tried using SoftAssertions in AssertJ, but weirdly, it was not printing out all the json objects OR maybe, I don't really understand the checkThat() method properly.

Tried using collectors.checkThat, couldn't get it to work reliably. This is a production area, so, don't have much room for errors, and wanna reduce the manual effort.

Made another attempt to use collectors as one of the posts on stackoverflow, couldn't get it to work reliably

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

Can someone please help ?

1 Answers1

0

If you want to gather all assertion errors and not stop at the first error then soft assertions is a good candidate to use. To get started with soft assertions you can follow the guide available here: https://assertj.github.io/doc/#assertj-core-soft-assertions.

collector.checkThat does not come from AssertJ (neither anything from your code samples), it's a bit confusing, I would suggest to write a reproducible test so that people can help more easily.

Alternatively if you are dealing with JSON, you can give a try to addressed by https://github.com/lukas-krecan/JsonUnit which provides first class citizen JSON assertions.

Hope it helps.

Joel Costigliola
  • 6,308
  • 27
  • 35