测试消费者 Kafka SpringBoot

时间:2021-03-22 14:26:17

标签: spring-boot testing junit apache-kafka

我需要帮助,如何使用 junit 测试我的项目? 我使用 Kafka 和 springBoot 我有我的消费者配置:

@EnableKafka
@Configuration
@Component
public class ConsumerCAPMDRConfir {

    @Autowired
    PropertyConfig propertyConfig;

    private final static String SASL_PROTOCOL = "SASL_SSL";
    private final static String SCRAM_SHA_256 = "SCRAM-SHA-256";
    private final String jaasTemplate = "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"%s\" password=\"%s\";";
    private final String consJaasCfg = String.format(jaasTemplate, "user", "pass");


    @Bean
    public ConsumerFactory<String, Topic_CAP_MDR> DtoConsumerCapMDR() {

        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, propertyConfig.getBootstrapServer());
        props.put(ConsumerConfig.GROUP_ID_CONFIG, propertyConfig.getGroupId());
        
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,io.confluent.kafka.serializers.KafkaAvroDeserializer.class.getName());
        props.put(KafkaAvroDeserializerConfig.SPECIFIC_AVRO_READER_CONFIG, true);
        
        if(propertyConfig.getFlag())
        {
        props.put("sasl.mechanism", SCRAM_SHA_256); 
        props.put("sasl.jaas.config", consJaasCfg); 
        props.put("security.protocol", SASL_PROTOCOL); 
        props.put("ssl.truststore.location", propertyConfig.getTruststore_location()); 
        props.put("ssl.truststore.password", propertyConfig.getPasswordTrustore()); 
        props.put("ssl.endpoint.identification.algorithm", ""); 
        props.put("schema.registry.url", propertyConfig.getSchema_registry());
        
        }
        
        
        return new DefaultKafkaConsumerFactory<>(props);
        
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, Topic_CAP_MDR> TopicCAPMDRListener() {
        ConcurrentKafkaListenerContainerFactory<String, Topic_CAP_MDR> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(DtoConsumerCapMDR());
        return factory;
    }
}

我的班级服务与此监听器:

@Service
public class CAPMDRServices {
    
    private static final Logger LOGGER = LogManager.getLogger(CAPMDRServices.class);
    
    @Autowired
    DataBaseConfig databaseconfig;
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    
    @KafkaListener(topics = "topic", containerFactory = "TopicCAPMDRListener")
    public void publish(Topic_CAP_MDR CAPMDR) {
        
        
        //INSERT HERE DB
    

     }
    }

在这一点上,我不知道如何实现这个测试: 与主题的联系...我需要为消费者测试声明一个生产者吗?

0 个答案:

没有答案