【发布时间】:2021-03-23 10:13:16
【问题描述】:
我正在尝试使用 mockmvc 进行测试,但失败并显示以下错误消息:
引起:org.apache.kafka.common.config.ConfigException
我们的服务层中有 Kafka 作为依赖项,它在我们正在测试的方法中被调用。
有没有办法在测试期间忽略该特定呼叫?
在下面的示例中,我们希望在测试期间忽略 notifyHrcOfInfectedUser()。
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);
}
【问题讨论】:
标签: java spring-boot testing apache-kafka mockmvc