Ajay C.V.
Ajay C.V.

Reputation: 13

Arguments are different error in Verify method because input parameter is of type Timestamp

I am trying to test a Spring JPA repository by mocking the repository. The input parameter for the repository is of type long and timestamp.

The problem is arising because of the difference in timestamp input between the 2 calls. I tried to use argThat but could not use it for timestamp object. Is there any other way to resolve this issue?

`@Before
public void setUp(){
    MockitoAnnotations.initMocks(this);
    collActivity = new CollActivity();
    Long time = System.currentTimeMillis();     
    Timestamp lastUpdate = new java.sql.Timestamp(time);
    collActivity.setCollId(1162L);
    collActivity.setGId(new BigDecimal(25));
    collActivity.setLastUpdate(lastUpdate);     
}`

`@Test
public void testUpdateCollActivity(){   
        Long collectionID = 5044L;      
        Long time = System.currentTimeMillis();                 
        when(typeRepo.getCollId(new Long(100033334L))).thenReturn(new 
     BigDecimal(2244));
        when(collRepository.updateCollActivity(collID, 
     lastUpdate)).thenReturn(1);            
        int i = collAcivityController.updateCollActivity("1000234");
     verify(collRepository).updateCollActivity(collID,
                                           collActivity.getLastUpdate());
}

The JUnit test fails at the verify call because the timestamp has a minor difference when the call is made to the repository with the error as below:

Argument(s) are different! Wanted: collRepository.updateCollActivity( 5044, 2019-10-28 12:44:49.738 );

Actual invocation has different arguments: collRepository.updateCollActivity( 5044, 2019-10-28 12:44:49.777 );

Upvotes: 0

Views: 480

Answers (1)

yash sugandh
yash sugandh

Reputation: 628

You can use Mockito.any() in these types of cases:

@Test
public void testUpdateCollActivity(){   
        Long collectionID = 5044L;      
        Long time = System.currentTimeMillis();                 
        when(typeRepo.getCollId(Mockito.anyLong())).thenReturn(new 
     BigDecimal(2244));
        when(collRepository.updateCollActivity(Mockito.anyLong(), 
     Mockito.anyLong())).thenReturn(1);            
        int i = collAcivityController.updateCollActivity("1000234");
     verify(collRepository).updateCollActivity(collID,
                                           collActivity.getLastUpdate());
}

You can also just use Mockito.any() if you are not sure about the dataType.

Upvotes: 1

Related Questions