mardi 23 mars 2021

Spring Boot - Ignoring a method call inside a method that is being tested

I am trying to make a test pass with mockmvc and it is failing with the following error message:

Caused by: org.apache.kafka.common.config.ConfigException

We have Kafka in our service layer as a dependency, and it is being called inside the method we are testing. Is there a way to ignore that specific call during tests? In the example below, we want to ignore the notifyHrcOfInfectedUser() during testing.

public UserDto changeInfectionStatus(String deviceId) {
    User user = this.userRepository.findById(deviceId)
            .orElseThrow(() -> new EntityNotFoundException("Could not find user with id " + deviceId));

    if (!hasPassedTwoWeeksMinimumRecoveryTime(user))
        throw new InfectionStatusException("Unable to change infection status since it has not been at least" +
                " two weeks since the last change.");

    UserDto updatedUser = updateStatus(user).convertToDto();
    notifyHrcOfInfectedUser(updatedUser.isInfected(), deviceId); // <-- Ignore this call during tests
    return updatedUser;
}

private void notifyHrcOfInfectedUser(boolean isInfected, String deviceId) {
    if (isInfected)
        kafkaSender.publish("infection-contact", deviceId);
}

Aucun commentaire:

Enregistrer un commentaire