Monthly Archives: February 2010

Testing XML data in your unit tests with XMLUnit

Once upon a time I had to compare two pieces of XML data. And it was good.

Well, actually, it wasn’t. Comparing two sets of XML can be really tough. Initially, I considered doing so by hand. Bad, bad idea, because:

  • You can’t simply say xmlString.equals(xmlString2) – any white space, including those indentation of yours will make the test fail;
  • The same equals call mentioned above will never say that <mytag></mytag> is the same as <mytag/> – and it almost always is;
  • How would you consider namespaces at all in those circumstances?
  • To solve all of the problems above, you could parse the XML data and verify each node… manually. A whole lot of work.

equals is totally out of the game for the reasons mentioned above. And I’m too lazy to check manually each node of the XML data. Also, such verification would be very error prone.

A better solution is needed, and after some ‘googling’ around, I found XMLUnit. In summary, what it does is that manual verification… But well tested and guaranteed to work – it is a framework developed specifically with that purpose in mind.

This is how a JUnit test using XMLUnit looks like:

import org.junit.Test;
import static org.custommonkey.xmlunit.XMLAssert.*;

public class SampleJCrankyTest {
    @Test
    public void testXml() {
        // this test will pass =)
        asssertXMLEqual("<myxml></myxml>", "</myxml>");
    }
}

This code is using JUnit and, obviously, java – but they seem to be developing a .Net version as well, if you prefer that – its just not as complete as the Java version.

There is a lot of additional xml related features supported, so a look in the documentation is recommended. But concluding, if you need to make xml comparisons, please do yourself a favour and don’t even consider doing any kind of manual testing – go for XMLUnit.

Have you ever faced this kind of problem before? How did you solve it? Please leave a comment! =)


Iced Earth in Brazil

Hi everybody! This post is EXTREMELY off-topic and contains no technical information, so feel free to skip it – I just couldn’t help it but write this one!

This last Saturday, February 6th, Iced Earth came to São Paulo – Brazil. Iced Earth is one of the greatest metal bands I know, and I had to go =)

Following I’ll post a few pictures from the event. Before that, I just have to say: Matthew Barlow is amazing. The guy sings like no one else – his live performance is as good, if not better, than the CDs. In summary: I WANT MORE !!

Iced Earth

Iced Earth

Iced Earth

Iced Earth

Iced Earth

Iced Earth

Iced Earth

Iced Earth


Pair Programming as a Motivator

Everyone has ups and downs. Maybe a fight with a significant other, or a problem with family, it doesn’t matter, brought you down. I went through something in this line a while ago, although for a completely different reason. But why am I talking about this?

People who read my blog for sometime now probably know I like agile software development. One of the strategies it uses (from eXtreme Programming) is pair programming, and I talked about it a long while ago here.

Now, to the point. What I found out is that, at least for me, pair programming has one more advantage that I was not used to pay attention to: it can help you get motivated. Just having to explain my ideas and having to listen to my pair’s ideas goes a long way in terms of motivation. As I love what I do, this conversation “thing” helps me get back to reality and be productive.

If you cannot do pair programming constantly, what I would suggest you should do is, as soon as you’re feeling you’re not doing as well as you could, do some short pairing. Even if at least for an hour or so, it should help renovate your thoughts.

Now get your hands dirt, try it out and post a comment with your opinion! =)


Follow

Get every new post delivered to your Inbox.