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); }
Advertisement
Answer
Are you able to mock the kafkaSender object? Then we can do something like…
final KafkaSender mockKafkaSender = Mockito.mock(KafkaSender.class); Mockito.doNothing().when(mockKafkaSender).publish(any(),any());
Update…
Or to be more accurate
Mockito.doNothing().when(mockKafkaSender).publish(eq("infection-contact"),eq(expectedDeviceId));