0

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;
...
}
Deniss M.
  • 3,617
  • 17
  • 52
  • 100

1 Answers1

1

Actually, you are not asserting. In your case, you are only verifying 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.

Yuri
  • 1,748
  • 2
  • 23
  • 26
  • I have done this: `assertThat(mockTransactRepViewRepository .findByClientIdAndClDateBetween("123456", todayDateTime.toDate(), todayDateTime.plusDays(1).toDate()).get(0)) .isSameAs(transactRepViewModelTest);` but even if I change the value of model client id to some other value, the test still is not failing. – Deniss M. Sep 13 '16 at 09:00
  • Sure thing, that's because **isSameAs** - checks if objects are same (e.g. checking if objects point to the same reference) – Yuri Sep 13 '16 at 09:02
  • Give a try to **isEqualTo** — checks if objects are equal (e.g. checking if objects are *equal* based on value) – Yuri Sep 13 '16 at 09:03
  • I have used isEqualTo and it also returns success, even though the code is like this: `TransactRepViewModel transactRepViewModelTest = new TransactRepViewModel(); transactRepViewModelTest.setClientId("00"); transactRepViewModelTest.setBatchDate(todayDateTime.toDate()); mockTransactRepViewRepository.save(transactRepViewModelTest);` and `assertThat(mockTransactRepViewRepository.findByClientIdAndClDateBetween("123456", todayDateTime.toDate(), todayDateTime.plusDays(1).toDate()).get(0)).isEqualTo(transactRepViewModelTest);` – Deniss M. Sep 13 '16 at 09:08
  • 1
    Oh, it seems that you need something like this http://stackoverflow.com/questions/1142837/verify-object-attribute-value-with-mockito – Yuri Sep 13 '16 at 09:12
  • Seems to be it. But is this the correct way to test such methods and objects? – Deniss M. Sep 13 '16 at 09:14
  • 1
    Actually, it is not. You have a repository layer that interacts with the database. But you are mocking everything here :) From my point of view, such tests could be useless over time as you will get stable result almost always. Unit tests are good if you are testing algorithms or something similar, but in this case, it is not the way to go. You should think about integration tests. I recommend you to have a look at http://dbunit.sourceforge.net/. This thing could prepare the database for some tests and you could write the expected database. – Yuri Sep 13 '16 at 09:18
  • 1
    You could also take a look at jhipster project http://jhipster.github.io/. https://github.com/jhipster/jhipster-sample-app/blob/master/src/test/java/io/github/jhipster/sample/service/UserServiceIntTest.java Here they are preparing test database (hsqldb) and do all test with real methods without mocks. Hope that it will help you! :) – Yuri Sep 13 '16 at 09:21
  • Thanks Yuri. The reason I ended up not using DBUnit is because I do not want to touch the real DB. And while trying to use DBUnit I encountered quite a lot of errors, since the DB I use has a lot of duplicate tables and etc. – Deniss M. Sep 13 '16 at 09:25
  • 1
    Yes, that's exactly. You have to touch real db. Moreover, it would be great if each developer will have its own test db to touch it. **But** you have another option like jhipster does. They are using an in-memory db for tests. This could be a really handy option if you have a clean new project with pretty straightforward SQL-queries. – Yuri Sep 13 '16 at 09:27
  • Thanks a lot for your replies. I'll see what I can do in the end! – Deniss M. Sep 13 '16 at 09:29