Wednesday, August 25, 2010

XML and Unit testing

I had to verify some XML using simple a String to verify. The code is the following:

public class FetchMessageServiceTest {
...
private static final String XML_INCOMING = "secretpassword876512345098765432155225testing 1,2";

@Test
public void shouldReturnAnXml() throws SAXException, IOException {
FetchMessageService service = new FetchMessageService(jmsTemplate, incomingService);
assertNotNull(service);
Redirection redirectionMock = getRedirection();
String xml = service.getXml(redirectionMock);
assertNotNull(xml);
assertEquals(XML_INCOMING, xml);
}


When I ran the Junit task in Eclipse, everything was fine. But when I ran it in Junit Ant task, I got the following error:

[junit] expected:<<?xml version=["1.0" encoding="UTF-8"]?>    [junit] <redirection><aut...> but was:<<?xml version=['1.0' encoding='UTF-8']?>
[junit] <redirection><aut...>
[junit] <redirection><aut...> but was:<<?xml version=['1.0' encoding='UTF-8']?>
[junit] <redirection><aut...>


I then decided to use XMLUnit. Once I end up doing this, everything worked perfect. I am not sure if it is something with the output of the Ant or if it's something with String. The code is the following:

@Test
public void shouldReturnAnXml() throws SAXException, IOException {
FetchMessageService service = new FetchMessageService(jmsTemplate, incomingService);
assertNotNull(service);
Redirection redirectionMock = getRedirection();
String xml = service.getXml(redirectionMock);
assertNotNull(xml);
XMLAssert.assertXMLEqual(XML_INCOMING, xml);
}


Everything seems to be working fine. I'm not sure if the problem with the test was with the output of Ant. I believe that it might be something to do with the output of the console and the encoding that it uses, but either way XMLUnit seems to be a great candidate for testing any type of XML regardless of the format.

No comments:

Post a Comment