我对 junit 测试用例很陌生,我想为 kafka 集成编写 junit。下面是我的代码
@EnableKafka
@Configuration
public class KafkaConfiguration {
@Value("${bts_config}")
private String bts_config;
@Value("${grpname}")
private String grpname;
@Bean
public ProducerFactory<String, String> producerFactory() throws URISyntaxException{
Map<String, Object> config = new HashMap<>();
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bts_config);
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put("security.protocol", "SSL");
config.put("ssl.protocol", "SSL");
config.put("ssl.truststore.location", "/dan/client.truststore.jks");
config.put("ssl.endpoint.identification.algorithm", "https");
return new DefaultKafkaProducerFactory(config);
}
@Bean
public KafkaTemplate<String, String> kafkaTemplate() throws URISyntaxException {
return new KafkaTemplate<String, String>(producerFactory());
}
}
在其他课程中,我有一种方法可以使用 kafkaTemplate 将消息发送到 kafka
public void pushMsgsKafaka(String topicName, String msg) {
ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(topicName, msg);
future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
@Override
public void onSuccess(SendResult<String, String> result) {
//success
}
@Override
public void onFailure(Throwable ex) {
LOGGER.error("Unable to send message=["
+ msg + "] due to : " + ex.getMessage());
}
});
}
现在我想为方法编写测试用例KafkaConfig (@EnableKafka, @Configuration)
和测试用例public void sendDataToKafka(String topicName, String msg)
我是编写junit测试用例的新手,有人可以帮我吗