简体   繁体   中英

spring-boot: mockito ignoring test criteria and returning passed test

I have the mentioned below test class utilizing Mockito. The problem is that it seems to ignore my setClientId parameter. If I set it to anything in:

when(mockTransactRepViewRepository.findByClientIdAndBatchDateBetween("SETRANDOMHERE", todayDateTime.toDate(), todayDateTime.plusDays(1).toDate()))
                    .thenReturn(Arrays.asList(transactRepViewModelTest, transactRepViewModelTest2));

the tests still pass. Shouldn't they fail? Or am I mistunderstanding something in Mockito?

Test class

@RunWith(MockitoJUnitRunner.class)
@SpringApplicationConfiguration(classes = TransactRepViewRepository.class)
public class TransactRepViewRepositoryTest {

    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock
    private TransactRepViewRepository mockTransactRepViewRepository;

    @Test
    public void testFindByClientIdAndBatchDateBetween() {
        DateTime todayDateTime = new DateTime().withTimeAtStartOfDay();

        TransactRepViewModel transactRepViewModelTest = new TransactRepViewModel();
        transactRepViewModelTest.setClientId("123456");
        transactRepViewModelTest.setBatchDate(todayDateTime.toDate());
        mockTransactRepViewRepository.save(transactRepViewModelTest);

        TransactRepViewModel transactRepViewModelTest2 = new TransactRepViewModel();
        transactRepViewModelTest2.setClientId("123456");
        transactRepViewModelTest2.setBatchDate(todayDateTime.plusDays(1).toDate());
        mockTransactRepViewRepository.save(transactRepViewModelTest2);

        when(mockTransactRepViewRepository.findByClientIdAndBatchDateBetween("123465", todayDateTime.toDate(), todayDateTime.plusDays(1).toDate()))
                .thenReturn(Arrays.asList(transactRepViewModelTest, transactRepViewModelTest2));
        verify(mockTransactRepViewRepository, times(1)).save(transactRepViewModelTest);
        verify(mockTransactRepViewRepository, times(1)).save(transactRepViewModelTest2);
    }

Just in case, this is the Repository class:

public interface TransactRepViewRepository extends JpaRepository <TransactRepViewModel, Long> {
...
List<TransactRepViewModel> findByTerminalnameIgnoreCaseContainingAndClDateBetween(String terminalName, Date startDate, Date endDate) throws DataAccessException;
...
}

Actually, you are not assert ing. In your case, you are only verify ing that the particular method was called . And that's why you have success in this case. You either have to add assert or you could, for example, change verify .

Instead of:

verify(mockTransactRepViewRepository, times(1)).save(transactRepViewModelTest);

You could do something like this:

verify(mockTransactRepViewRepository, times(2)).save(transactRepViewModelTest);

Here you are verify (and you get an error here) that your method was called only once, but not twice .

Quick Note

I am seeing that you are writing Spring Boot tests. Please, take a look here how to write tests properly. By the way, there are a lot of additional helpers were added. You could take a look at it at Spring blog .

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