在测试中找不到 KafkaProperties bean
KafkaProperties bean not found in test
我的 kafka 有问题,当我启动应用程序时它很好但是当我 运行 测试它找不到 bean KafkaProperties。
Description:
Parameter 1 of constructor in ***.session.config.KafkaConfig required a bean of type 'org.springframework.boot.autoconfigure.kafka.KafkaProperties' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.boot.autoconfigure.kafka.KafkaProperties' in your configuration.
当我启动应用程序时,它会注入 KafkaProperties,但在测试期间它不会。
测试 class 开始于,
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = SessionController.class)
public class SessionControllerTest {
卡夫卡配置,
@RequiredArgsConstructor
@Configuration
public class KafkaConfig {
private final KafkaProducerProps kafkaProducerProps;
private final KafkaProperties kafkaProperties;
@Bean
public ProducerFactory<String, SessionEvent> producerFactory() {
Map<String, Object> configProps = new HashMap<>();
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProducerProps.getBootstrapServers());
configProps.put(ProducerConfig.CLIENT_ID_CONFIG, kafkaProducerProps.getClientId());
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configProps.put(
AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG,
kafkaProperties.getProperties().get(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG)
);
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class);
configProps.put(AbstractKafkaAvroSerDeConfig.AUTO_REGISTER_SCHEMAS, true);
return new DefaultKafkaProducerFactory<>(configProps);
}
@Bean
public KafkaTemplate<String, SessionEvent> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
@Bean
public KafkaMessageSender kafkaMessageSender() {
return new KafkaMessageSender(kafkaTemplate(), kafkaProducerProps.getDefaultTopic());
}
}
测试/../application.yml
spring:
main:
allow-bean-definition-overriding: true
kafka:
bootstrap-servers: ${KAFKA_BROKERS:server_ip:9092}
client-id: ${KAFKA_CLIENT_ID:session-service}
template:
default-topic: ${KAFKA_TOPIC:topic_name}
properties:
schema.registry.url: ${KAFKA_REGISTRY_URL:kafka_registry_url/}
谢谢。
您需要使用 @SpringBootTest
才能在测试用例中获得 Boot 的自动配置优势。
我的 kafka 有问题,当我启动应用程序时它很好但是当我 运行 测试它找不到 bean KafkaProperties。
Description:
Parameter 1 of constructor in ***.session.config.KafkaConfig required a bean of type 'org.springframework.boot.autoconfigure.kafka.KafkaProperties' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.boot.autoconfigure.kafka.KafkaProperties' in your configuration.
当我启动应用程序时,它会注入 KafkaProperties,但在测试期间它不会。
测试 class 开始于,
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = SessionController.class)
public class SessionControllerTest {
卡夫卡配置,
@RequiredArgsConstructor
@Configuration
public class KafkaConfig {
private final KafkaProducerProps kafkaProducerProps;
private final KafkaProperties kafkaProperties;
@Bean
public ProducerFactory<String, SessionEvent> producerFactory() {
Map<String, Object> configProps = new HashMap<>();
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProducerProps.getBootstrapServers());
configProps.put(ProducerConfig.CLIENT_ID_CONFIG, kafkaProducerProps.getClientId());
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configProps.put(
AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG,
kafkaProperties.getProperties().get(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG)
);
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class);
configProps.put(AbstractKafkaAvroSerDeConfig.AUTO_REGISTER_SCHEMAS, true);
return new DefaultKafkaProducerFactory<>(configProps);
}
@Bean
public KafkaTemplate<String, SessionEvent> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
@Bean
public KafkaMessageSender kafkaMessageSender() {
return new KafkaMessageSender(kafkaTemplate(), kafkaProducerProps.getDefaultTopic());
}
}
测试/../application.yml
spring:
main:
allow-bean-definition-overriding: true
kafka:
bootstrap-servers: ${KAFKA_BROKERS:server_ip:9092}
client-id: ${KAFKA_CLIENT_ID:session-service}
template:
default-topic: ${KAFKA_TOPIC:topic_name}
properties:
schema.registry.url: ${KAFKA_REGISTRY_URL:kafka_registry_url/}
谢谢。
您需要使用 @SpringBootTest
才能在测试用例中获得 Boot 的自动配置优势。