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">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

4

1 回答 1

5

因此,您在代码中生成 XML,但使用类似new Date()获取当前日期/时间的方法?我建议您将时钟作为依赖项注入,就像任何其他依赖项一样。这样您就可以控制您的代码认为“现在”的时间,并拥有可预测的 XML 进行测试。

事实证明,在我的经验中,注入时钟非常有用——而且非常容易做到。我建议使用和(用于生产)和(用于测试)Clock的实现接口。SystemClockFakeClock

无论您以毫秒表示当前时间,Date还是(我的偏好)来自 Joda Time 的某些内容,这都是隔离依赖关系的好方法。

于 2010-06-24T21:45:53.453 回答