lundi 19 mars 2018

How to properly check XML serialization with AssertJ?

I need to test my REST application - I need to check XML serialization, so I've created test:

@Test
public void XMLTest() throws Exception {
    RequestBuilder requestBuilder = MockMvcRequestBuilders
            .get("/api/clients/xml/1")
            .accept(MediaType.APPLICATION_XML);
    MvcResult result = mvc.perform(requestBuilder).andReturn();
    String expected = "<Application>\n" +
            "    <applicationId>101</applicationId>\n" +
            "    <contactId>1</contactId>\n" +
            "    <dtCreated>\n" +
            "        <hour>18</hour>\n" +
            "        <minute>47</minute>\n" +
            "        <second>52</second>\n" +
            "        <nano>0</nano>\n" +
            "        <dayOfYear>260</dayOfYear>\n" +
            "        <dayOfWeek>WEDNESDAY</dayOfWeek>\n" +
            "        <month>SEPTEMBER</month>\n" +
            "        <dayOfMonth>17</dayOfMonth>\n" +
            "        <year>2014</year>\n" +
            "        <monthValue>9</monthValue>\n" +
            "        <chronology>\n" +
            "            <id>ISO</id>\n" +
            "            <calendarType>iso8601</calendarType>\n" +
            "        </chronology>\n" +
            "    </dtCreated>\n" +
            "    <productName>Product1</productName>\n" +
            "</Application>";
    String actual = result.getResponse().getContentAsString();
    assertThat(expected).isXmlEqualTo(actual);
    }

So, everything is fine except that Spring MVC serializes object in a random form, so I can't know for sure in which order XML document will be formed, like this one:

 <Application>
<applicationId>101</applicationId>
<contactId>1</contactId>
<dtCreated>
    <dayOfYear>260</dayOfYear>
    <dayOfWeek>WEDNESDAY</dayOfWeek>
    <month>SEPTEMBER</month>
    <dayOfMonth>17</dayOfMonth>
    <year>2014</year>
    <monthValue>9</monthValue>
    <hour>18</hour>
    <minute>47</minute>
    <second>52</second>
    <nano>0</nano>
    <chronology>
        <id>ISO</id>
        <calendarType>iso8601</calendarType>
    </chronology>
</dtCreated>
<productName>Product1</productName>
</Application>

or like this one:

<Application>
<applicationId>101</applicationId>
<contactId>1</contactId>
<dtCreated>
    <hour>18</hour>
    <minute>47</minute>
    <second>52</second>
    <nano>0</nano>
    <dayOfYear>260</dayOfYear>
    <dayOfWeek>WEDNESDAY</dayOfWeek>
    <month>SEPTEMBER</month>
    <dayOfMonth>17</dayOfMonth>
    <year>2014</year>
    <monthValue>9</monthValue>
    <chronology>
        <id>ISO</id>
        <calendarType>iso8601</calendarType>
    </chronology>
</dtCreated>
<productName>Product1</productName>
</Application>

As you can see field is in a different part of document in these two cases, so sometimes my test is passing okay, sometimes it's failing, because I can't predict which serialization order Spring MVC will use - is that possible to fix that ? I've already read AssertJ documentation - it says nothing about order ignoring or something like that. Please, give me hint.

Aucun commentaire:

Enregistrer un commentaire