简体   繁体   中英

JUnit: allow 1 second difference between expected and actual

I am developping something which keeps the current age of the oldest item, for code coverage I write JUnit tests.

So when I write my unit tests, I perform following action:

currentAge = resultSet.getTime(4);
Calendar cal = Calendar.getInstance();
        Assert.assertEquals(new Time(cal.get(Calendar.HOUR_OF_DAY) - 1,cal.get(Calendar.MINUTE) - 30,Calendar.SECOND), currentAge);

This actualy works, but I get sometimes this test failure

junit.framework.AssertionFailedError:
Expected :08:09:14

Actual :08:09:13

I think the reason is that the sequence of the actions maybe makes sometimes 1 second difference. Is there any way to round this in a good way or making the test to perform that 1 second difference is allowed?

What should I do?

在您的assertEquals方法中使用delta参数-请参见此答案此javadoc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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