Generated test is testing mock method instead of original method

Hi,

I just installed Diffblue and I am trying to use it to create tests on an existing class. I tried to create a test on the save method which should call the corresponding method from the repository class to save the object to the database. Diffblue creates the test, but the test doesn’t call the method that needs to be tested, it calls a mock instead.

Is this expected behavior? My expectation would be for the mock repository’s save method to be defined and for the save method to be called from the service.

Here is the example of generated test:

    @Test
    void testSave() {
        AccountRoleDTO accountRoleDTO = new AccountRoleDTO();
        accountRoleDTO.setCode("Code");

        AccountRoleAssignedPrivDTO accountRoleAssignedPrivDTO = new AccountRoleAssignedPrivDTO();
        accountRoleAssignedPrivDTO.setId(1L);
        accountRoleAssignedPrivDTO.setPrivilege(AccountSecurityPrivilege.ACCOUNT_ALL_R);
        accountRoleAssignedPrivDTO.setRole(accountRoleDTO);
        when(accountRoleAssignedPrivService.save((AccountRoleAssignedPrivDTO) any()))
            .thenReturn(accountRoleAssignedPrivDTO);

        AccountRoleDTO accountRoleDTO1 = new AccountRoleDTO();
        accountRoleDTO1.setCode("Code");

        AccountRoleAssignedPrivDTO accountRoleAssignedPrivDTO1 = new AccountRoleAssignedPrivDTO();
        accountRoleAssignedPrivDTO1.setId(1L);
        accountRoleAssignedPrivDTO1.setRole(accountRoleDTO1);
        assertSame(accountRoleAssignedPrivDTO, accountRoleAssignedPrivService.save(accountRoleAssignedPrivDTO1));
        verify(accountRoleAssignedPrivService).save((AccountRoleAssignedPrivDTO) any());
    }

here is the save method that should be tested:

    public AccountRoleAssignedPrivDTO save(AccountRoleAssignedPrivDTO accountRoleAssignedPrivDTO) {
        log.debug("Request to save AccountRoleAssignedPriv : {}", accountRoleAssignedPrivDTO);
        AccountRoleAssignedPriv accountRoleAssignedPriv = accountRoleAssignedPrivMapper.toEntity(accountRoleAssignedPrivDTO);
        accountRoleAssignedPriv = accountRoleAssignedPrivRepository.save(accountRoleAssignedPriv);
        return accountRoleAssignedPrivMapper.toDto(accountRoleAssignedPriv);
    }

Hi ivrbovcan,

Thnak you for your message. Please let us know if you tried running Diffblue cover with the “Enable integration tests” option enabled.

This should reduce the amount of mocking. Please let us know if your issues is resolved or if you need more help.

Kind regards.
Cristian