简体   繁体   English

Java spring assertEquals 语句中经过的瞬间时间

[英]Java spring time of instants elapsed in assertEquals statement

I have some tests in my testsuite that run on all my colleagues, but fail on me.我的测试套件中有一些测试在我所有的同事身上运行,但在我身上却失败了。 And that is to return two times from different entities in the form of an instant and compare them with each other.那就是以瞬间的形式从不同的实体返回两次,并相互比较。 They actually match, but not for me.他们实际上匹配,但不适合我。

This is the described line:这是描述的行:

assertEquals(notUpdatedRule.getModStamp(), rule.getModStamp());

As error message I get back the following:作为错误消息,我得到以下信息:

org.opentest4j.AssertionFailedError: 
Expected :2023-01-22T19:46:20.754829Z
Actual   :2023-01-22T19:46:20.754829486Z

I have already tried to adapt my openjdk version to that of my colleagues, but this has not changed anything.我已经尝试使我的 openjdk 版本适应我同事的版本,但这并没有改变任何东西。

I recommend that you use Instant.now(Clock clock) instead.我建议您改用Instant.now(Clock clock) From its JavaDocs : "Using this method allows the use of an alternate clock for testing."来自其JavaDocs :“使用此方法允许使用备用时钟进行测试。”

The problems could arise from different time zones, different operating systems, different hardware precision of timers in your systems etc.问题可能来自不同的时区、不同的操作系统、系统中计时器的不同硬件精度等。

When you specify a Clock you could create that with a defined precision (milliseconds, seconds, minutes or any other Duration ) for your tests.当您指定Clock时,您可以为您的测试创建具有定义精度(毫秒、秒、分钟或任何其他Duration )的时钟。

Or you could provide a Clock that always returns the same instant for your tests (which I really would recommend to have reproducible tests!) with Clock.fixed(Instant fixedInstant, ZoneId zone) .或者,您可以使用Clock.fixed(Instant fixedInstant, ZoneId zone)提供始终为您的测试返回相同时刻的Clock (我真的建议进行可重现的测试!)。

For production use, you can simply provide a Clock.systemUTC() to have the current behaviour of Instant.now() .对于生产用途,您可以简单地提供一个Clock.systemUTC()来获得Instant.now() () 的当前行为。 Clock s JavaDocs explain the reason for using it in more detail. Clock的 JavaDocs更详细地解释了使用它的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM