'How to compare new Date in unit test?

I have service with method :

Entity entity = new Entity(new Date(), 1, 2L);
return entityRepository.save(entity);

And my test :

@Test
public void testSaveEntity() {
    Entity entity = new Entity(new Date(), 1, 2L);
    entityService.saveEntity(1, 2L);
    verify(entityRepository, times(1)).save(entity);
} 

If Entity equals() not compared Date then everything is all right but if compare Date then test throws out Argument(s) are different!



Solution 1:[1]

You have 2 options:

Option 1: Use Clock class to control time

Instead of using new Date():

  • inject a Clock instance to your service
  • use its methods to retrieve current time
  • In your test code, use Clock.fixed to control current time

See Guide to the Java Clock Class

Option 1: Relax your matching requirements

Use Mockito ArgumentMatchers to relax mtching requirements - use any(YourEntity.class) to match any YourEntity, or write a custom argument matcher for your Entity.

Solution 2:[2]

You may be having an issue with the definition of your equals method. You should define under which circumstances two different Entities are considered equal:

Are two Entities equal only if their date value is within the same
millisecond or do we only care about seconds, minutes or days?

Similar to handling floating point values, this acceptable difference can be calculated similar to Calculating the difference between two Java date instances

Solution 3:[3]

Instead of relying on the equals method used in your solution:

verify(entityRepository, times(1)).save(entity);

you could try to capture the Argument and assert it in the next step as described in Verify object attribute value with mockito

ArgumentCaptor<Entity> argument = ArgumentCaptor.forClass(Entity.class);
verify(entityRepository, times(1)).save((argument.capture());
assertEquals(1, argument.getValue().getWhatever());

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 WizardLizard
Solution 3