tags:

views:

20

answers:

1

I have some XML which I am testing, which has set as one of it's elements the date and time. This obviously will change every time the test is run.

Is there a way to compare the XML and have this node value ignored, or just checked with a regular expression or something.

The XML I am testing is as follows:

<xml-fragment>
    <core:date xmlns:core="http://www.company.com/commerce/common/V1/core"&gt;2010-06-24T21:41:22.456+01:00&lt;/core:date&gt;
    <core:serviceName xmlns:core="http://www.company.com/commerce/common/V1/core"&gt;calculateNextAndFuturePayments
    </core:serviceName>
    <core:ackRequired xmlns:core="http://www.company.com/commerce/common/V1/core"&gt;false&lt;/core:ackRequired&gt;
    <v1:viewingCardNumber xmlns:v1="http://www.company.com/commerce/calculateNextAndFuturePayment/V1"&gt;405536053
    </v1:viewingCardNumber>
</xml-fragment>

I would like to check that the initial element (core:date) is in the date format, but the values are irrelevant for equality purposes.

Thanks

+2  A: 

So you're generating the XML in your code, but using something like new Date() to get the current date/time? I suggest you inject a clock as a dependency, just like any other dependency. That way you can control what time your code thinks is "now" and have predictable XML to test.

Injecting a clock has proved really useful in my experience - and incredibly easy to do. I'd recommend having a Clock interface with implementations along the lines of SystemClock and (for production) and FakeClock (for testing).

Whether you express the current time in millis, Date or (my preference) something from Joda Time, it's a good way of isolating the dependency.

Jon Skeet