lundi 23 mars 2015

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?


Aucun commentaire:

Enregistrer un commentaire