title: JUnit ~ Assert that list contains objects with the same values in any order
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>To abstract that, consider this example:</p>
  
  <pre><code>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&lt;Apple&gt; referenceList = new List&lt;Apple&gt;(referenceApple1, referenceApple2);
  List&lt;Apple&gt; correctlyParsedList1 = new List&lt;Apple&gt;(parsedApple1, parsedApple2);
  List&lt;Apple&gt; correctlyParsedList2 = new List&lt;Apple&gt;(parsedApple2, parsedApple1);
  List&lt;Apple&gt; wronglyParsedList1 = new List&lt;Apple&gt;(parsedApple1, badApple);
  List&lt;Apple&gt; wronglyParsedList2 = new List&lt;Apple&gt;(parsedApple1, parsedApple2, parsedApple1);
  List&lt;Apple&gt; wronglyParsedList3 = new List&lt;Apple&gt;(parsedApple2, parsedApple2);
  </code></pre>
  
  <p>I am looking for an assertion method that passes when comparing any of the above <code>correctlyParsedList*</code> with the <code>referenceList</code>, but fails when comparing any of the above <code>wronglyParsedList*</code> with the <code>referenceList</code>.</p>
  
  <p>Currently, the closest I've gotten to is this:</p>
  
  <pre><code>assertEquals(referenceList.toString(), correctlyParsedList1.toString())
  </code></pre>
  
  <p>However, that will fail as soon as the objects are in another order.</p>
  
  <pre><code>//Will fail, but I want a method that will compare these and pass
  assertEquals(referenceList.toString(), correctlyParsedList2.toString())
  </code></pre>
  
  <p>Notably, the following will also fail since the Apples, while containing the same values, are not instances of the same object:</p>
  
  <pre><code>assertThat(correctlyParsedList1, is(referenceList));
  
  //Throws This error:
  java.lang.AssertionError: 
  Expected: is &lt;[[color="red"], [color="green"]]&gt;
       but: was &lt;[[color="red"], [color="green"]]&gt;
  </code></pre>
  
  <p>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.</p>
  
  <hr>
  
  <p>EDIT ~ CONCRETIZATION</p>
  
  <p>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.</p>
  
  <p>So effectively, what I am comparing in this practical use case is two <code>List&lt;Attribute&gt;</code>, with <code>Attribute</code> coming from <code>org.jdom2.Attribute</code>.</p>
  
  <p>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:</p>
  
  <pre><code>    @Test
      public void importXML_fromFileShouldCreateXML_objectWithCorrectAttributes() throws JDOMException, IOException {
          testInitialization();
          List&lt;Attribute&gt; expectedAttributes = rootNode.getAttributes();
  
          XML_data generatedXML_data = xml_importer.importXML_fromFile(inputFile);
          List&lt;Attribute&gt; actualAttributes = generatedXML_data.attributes;
  
          assertEquals(expectedAttributes.toString(), actualAttributes.toString());
      }
  </code></pre>
  
  <p>The concrete error I get when trying to make that assertion with <code>assertThat(expectedAttributes, is(actualAttributes))</code> is:</p>
  
  <pre><code>java.lang.AssertionError: 
  Expected: is &lt;[[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"]]&gt;
      but: was &lt;[[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"]]&gt;
  </code></pre>