使用带有变量值的 XMLUnit 测试 XML

发布于 2024-09-07 03:07:32 字数 805 浏览 1 评论 0原文

我正在测试一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只涨不跌 2024-09-14 03:07:32

因此,您要在代码中生成 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文