I'm developing a project. This project has a repository (UserRepository
) which is referenced by other parts of the project, I want to know if I have to test the repository method each time it is referenced: For example, the method (repository.save()
) is used in more parts of the project, does it have to be tested each time?
My service:
@Transactional
public UpdateUserDTO update(UpdateUserDTO body) throws JsonMappingException {
User entity = repository.findByCpfCnpj(body.cpfCnpj()).orElseThrow(
() -> new EntityNotFoundException("ENTITY NOT FOUND"));
User userUpdated = mapper.updateValue(entity, body);
repository.save(userUpdated);
return mapper.convertValue(userUpdated, UpdateUserDTO.class);
}
My tests:
@Test
public void register_WithInvalidData_ReturnsThrowsException() {
var invalidUser = UserConstants.getInvalidUser();
assertThatThrownBy(() -> repository.save(invalidUser)).isInstanceOf(RuntimeException.class);
}
@Test
public void login_WithInvalidData_ReturnsThrowsException() {
assertThatThrownBy(() -> repository.save(new User())).isInstanceOf(RuntimeException.class);
}
update
method in this example. A test could also verifysave
was invoked. But assertingsave
throws is testing Spring, not your code.