Xetius
Xetius

Reputation: 46864

Testing XML with XMLUnit with a variable value

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">2010-06-24T21:41:22.456+01:00</core:date>
    <core:serviceName xmlns:core="http://www.company.com/commerce/common/V1/core">calculateNextAndFuturePayments
    </core:serviceName>
    <core:ackRequired xmlns:core="http://www.company.com/commerce/common/V1/core">false</core:ackRequired>
    <v1:viewingCardNumber xmlns:v1="http://www.company.com/commerce/calculateNextAndFuturePayment/V1">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

Upvotes: 1

Views: 911

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502446

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.

Upvotes: 5

Related Questions