使用带有变量值的 XMLUnit 测试 XML
我正在测试一些 XML,它已将日期和时间设置为其元素之一。这显然会在每次运行测试时发生变化。
有没有办法比较 XML 并忽略此节点值,或者仅使用正则表达式或其他内容进行检查。
我正在测试的 XML 如下:
<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>
我想检查初始元素 (core:date) 是否采用日期格式,但这些值与平等目的无关。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,您要在代码中生成 XML,但使用类似
new Date()
的方法来获取当前日期/时间?我建议您注入时钟作为依赖项,就像任何其他依赖项一样。这样,您就可以控制代码认为“现在”的时间,并有可预测的 XML 进行测试。根据我的经验,注入时钟确实非常有用,而且非常容易做到。我建议使用一个
Clock
接口,其实现类似于SystemClock
and (用于生产)和FakeClock
(用于测试)。无论您以毫秒、日期还是(我的偏好)Joda Time 中的某些内容表示当前时间,这都是隔离依赖性的好方法。
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 ofSystemClock
and (for production) andFakeClock
(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.