简体   繁体   中英

JUnit ~ Assert that list contains objects with the same values in any order

My use case is that I am writing a document parser (for ReqIF documents) and want to compare that the parsed object contains the expected elements. For this, I want to write a JUnit Test.

Effectively, what I am looking for is an assertion method that compares two lists of objects with values and passes when both lists contain (in any order) objects that have the same values as the reference list.

To abstract that, consider this example:

Apple referenceApple1 = new Apple(color="red");
Apple referenceApple2 = new Apple(color="green");
Apple parsedApple1 = new Apple(color="red");
Apple parsedApple2 = new Apple(color="green");
Apple badApple = new Apple(color="brown");

List<Apple> referenceList = new List<Apple>(referenceApple1, referenceApple2);
List<Apple> correctlyParsedList1 = new List<Apple>(parsedApple1, parsedApple2);
List<Apple> correctlyParsedList2 = new List<Apple>(parsedApple2, parsedApple1);
List<Apple> wronglyParsedList1 = new List<Apple>(parsedApple1, badApple);
List<Apple> wronglyParsedList2 = new List<Apple>(parsedApple1, parsedApple2, parsedApple1);
List<Apple> wronglyParsedList3 = new List<Apple>(parsedApple2, parsedApple2);

I am looking for an assertion method that passes when comparing any of the above correctlyParsedList* with the referenceList , but fails when comparing any of the above wronglyParsedList* with the referenceList .

Currently, the closest I've gotten to is this:

assertEquals(referenceList.toString(), correctlyParsedList1.toString())

However, that will fail as soon as the objects are in another order.

//Will fail, but I want a method that will compare these and pass
assertEquals(referenceList.toString(), correctlyParsedList2.toString())

Notably, the following will also fail since the Apples, while containing the same values, are not instances of the same object:

assertThat(correctlyParsedList1, is(referenceList));

//Throws This error:
java.lang.AssertionError: 
Expected: is <[[color="red"], [color="green"]]>
     but: was <[[color="red"], [color="green"]]>

Is there a simple way to make such an assertion in JUnit? I know I could write a custom assertion method for this iterating over the objects, but somehow it feels like this would be a common use case that should have a pre-defined assertion method that throws expressive assertion errors.


EDIT ~ CONCRETIZATION

What I am actually trying to do with this abstract example is to parse a complex XML using JDOM2, and I want to assert that the attributes of a tag being parsed equal those that exist in the sample document I give as an input. Since this is XML, the order of the attributes is irrelevant, as long as they have the correct values.

So effectively, what I am comparing in this practical use case is two List<Attribute> , with Attribute coming from org.jdom2.Attribute .

The complete makeshift testcase with which I am currently unhappy because it will fail if the order of attributes changes but should not is as follows:

    @Test
    public void importXML_fromFileShouldCreateXML_objectWithCorrectAttributes() throws JDOMException, IOException {
        testInitialization();
        List<Attribute> expectedAttributes = rootNode.getAttributes();

        XML_data generatedXML_data = xml_importer.importXML_fromFile(inputFile);
        List<Attribute> actualAttributes = generatedXML_data.attributes;

        assertEquals(expectedAttributes.toString(), actualAttributes.toString());
    }

The concrete error I get when trying to make that assertion with assertThat(expectedAttributes, is(actualAttributes)) is:

java.lang.AssertionError: 
Expected: is <[[Attribute: xsi:schemaLocation="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd http://www.omg.org/spec/ReqIF/20110401/reqif.xsd"], [Attribute: xml:lang="en"]]>
    but: was <[[Attribute: xsi:schemaLocation="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd http://www.omg.org/spec/ReqIF/20110401/reqif.xsd"], [Attribute: xml:lang="en"]]>

You should use containsAll to check whether one list has all items from another, as long as you have proper equals implementation for your Apple this should work:

    // Covers case where all items that are from correct list are in reference as well
    // but also some additional items exist that are not in correct list
    assertEquals(correctlyPassedList.size(), referenceList.size());
    // Checks if each element is contained in list
    assertTrue(referenceList.containsAll(correctlyPassedList));

UPDATE

Regarding your latest comment probably this would work best:

    // Sort both lists using comparator based on id or whatever field is relevant
    referenceList.sort(Comparator.comparingInt(Apple::getId));
    correctlyParsedList.sort(Comparator.comparingInt(Apple::getId));
    // Check if they are equal
    assertEquals(correctlyParsedList, referenceList);

如果你不关心元素的顺序,你可以在比较它们之前对列表进行排序。

I personally prefer to always use AssertJ .

import static org.assertj.core.api.Assertions.assertThat;

Either of these two assertions should do the trick!

assertThat(correctlyParsedList1).containsOnlyOnce(referenceApple1, referenceApple2);
assertThat(correctlyParsedList1).containsOnlyOnceElementsOf(referenceList);

Drawing on @FilipRistic's answer, this is what worked for me:

Sort both lists using comparator based on id or whichever field is relevant:

referenceList.sort(Comparator.comparingInt(Apple::getId));
correctlyParsedList.sort(Comparator.comparingInt(Apple::getId));

Then simply:

assertThat(referenceList).usingRecursiveComparison().isEqualTo(correctlyParsedList);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM