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"]]>