Search code examples
javaspringjava-8integration-testingverify

how to verify a void function is been called in spring integration test


I have wrote an integration for testing a delete function like as shown below

@Test
void deleteUsersTest() {
    Map<String, String> params = new HashMap<>();
    params.put("id", "21");
    this.template.delete("/v1/users/{id}", params);
    :
}

The problem I am facing is, since it is a void function I would like to verify the below function is been called internally

userRepository.deleteById(21)

In Unit test I usually used something like this

verify(userRepository, times(1)).deleteById((long) 21);

but the above one is mockito based function which I cannot used in Integration test

Can someone help me on how to verify this function in Spring Integration test

I am using Spring 5, Spring Boot 2.1


Solution

  • Integration testing happens on a real database. Just ensure that the entity is stored in your database before calling delete and not stored in your database after calling delete.

    @BeforeEach
    public void setDatabase() {
        client1 = new Client();
        client1.setName("Karl");
        client2 = new Client();
        client2.setName("Pauline");
    
        testEntityManager.persist(client1);
        testEntityManager.persist(client2);
        testEntityManager.flush();
    }
    
    @Test
    public void deleteTest() {
        clientRepository.deleteById(client1.getId());
    
        List<Client> clientListActual = clientRepository.findAll();
        boolean clientExists = clientListActual.contains(client1);
    
        assertFalse(clientExists);
        assertEquals(1, clientListActual.size());
    }