How to write Unit test for @KafkaListener?

安稳与你 提交于 2019-12-04 19:37:38

You can wrap the listener in your test case.

Given

@SpringBootApplication
public class So52783066Application {

    public static void main(String[] args) {
        SpringApplication.run(So52783066Application.class, args);
    }

    @KafkaListener(id = "so52783066", topics = "so52783066")
    public void listen(String in) {
        System.out.println(in);
    }

}

then

@RunWith(SpringRunner.class)
@SpringBootTest
public class So52783066ApplicationTests {

    @ClassRule
    public static KafkaEmbedded embeddedKafka = new KafkaEmbedded(1, true, "so52783066");

    @Autowired
    private KafkaListenerEndpointRegistry registry;

    @Autowired
    private KafkaTemplate<String, String> template;

    @Before
    public void setup() {
        System.setProperty("spring.kafka.bootstrap-servers", embeddedKafka.getBrokersAsString());
    }

    @Test
    public void test() throws Exception {
        ConcurrentMessageListenerContainer<?, ?> container = (ConcurrentMessageListenerContainer<?, ?>) registry
                .getListenerContainer("so52783066");
        container.stop();
        @SuppressWarnings("unchecked")
        AcknowledgingConsumerAwareMessageListener<String, String> messageListener = (AcknowledgingConsumerAwareMessageListener<String, String>) container
                .getContainerProperties().getMessageListener();
        CountDownLatch latch = new CountDownLatch(1);
        container.getContainerProperties()
                .setMessageListener(new AcknowledgingConsumerAwareMessageListener<String, String>() {

                    @Override
                    public void onMessage(ConsumerRecord<String, String> data, Acknowledgment acknowledgment,
                            Consumer<?, ?> consumer) {
                        messageListener.onMessage(data, acknowledgment, consumer);
                        latch.countDown();
                    }

                });
        container.start();
        template.send("so52783066", "foo");
        assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
    }

}

how do I ensure @KafkaListener is invoked when a new message is arrived in Kafka.

Well, this is essentially a Framework responsibility to test such a functionality. In your case you need just concentrate on the business logic and unit test exactly your custom code, but not that one compiled in the Framework. In addition there is not goo point to test the @KafkaListener method which just logs incoming messages. It is definitely going to be too hard to find the hook for test-case verification.

On the other hand I really believe that business logic in your @KafkaListener method is much complicated than you show. So, it might be really better to verify your custom code (e.g. DB insert, some other service call etc.) called from that method rather than try to figure out the hook exactly for the myMessageListener().

What you do with the mock(MyMessageProcessor.class) is really a good way for business logic verification. Only what is wrong in your code is about that duplication for the EmbeddedKafka: you use an annotation and you also declare a @Bean in the config. You should think about removing one of them. Although it isn't clear where is your production code, which is really free from the embedded Kafka. Otherwise, if everything is in the test scope, I don't see any problems with your consumer and producer factories configuration. You definitely have a minimal possible config for the @KafkaListener and KafkaTemplate. Only what you need is to remove a @EmbeddedKafka do not start the broker twice.

Here is my working solution for the Consumer, based on your code. Thank you :-)

The Configuration is the following:

@TestConfiguration
@EnableKafka
@Profile("kafka_test")
public class KafkaTestConfig {

   private static Logger log = LoggerFactory.getLogger(KafkaTestConfig.class);

   @Value("${spring.kafka.bootstrap-servers}")
   private String bootstrapServers;

   @Bean
   @Primary
   public Map<String, Object> consumerConfigs() {
   Map<String, Object> props = new HashMap<>();
   props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
   props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
   props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
   props.put(ConsumerConfig.GROUP_ID_CONFIG, "group-id");
   props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
   props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
   props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 15000);

   log.info("Consumer TEST config = {}", props);
   return props;
  }

 @Bean
 public Map<String, Object> producerConfigs() {
     Map<String, Object> props = new HashMap<>();
     props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
     props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
     props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);

     log.info("Producer TEST config = {}", props);
      return props;
  }

  @Bean
  public ConsumerFactory<String, String> consumerFactory() {
      return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new 
      StringDeserializer(),
      new JsonDeserializer<String>());
  }

@Bean
 public ProducerFactory<String, String> producerFactory() {
   DefaultKafkaProducerFactory<String, String> pf =
      new DefaultKafkaProducerFactory<>(producerConfigs());
   return pf;
 }

 @Bean
  public ConcurrentKafkaListenerContainerFactory<String, String> 
 kafkaListenerContainerFactory(
   ConsumerFactory<String, String> kafkaConsumerFactory) {
  ConcurrentKafkaListenerContainerFactory<String, String> factory =
     new ConcurrentKafkaListenerContainerFactory<>();
  factory.setConsumerFactory(consumerFactory());
  factory.getContainerProperties().setAckOnError(false);
  factory.setConcurrency(2);
  return factory;
}

 @Bean
 public KafkaTemplate<String, String> kafkaTemplate() {
  KafkaTemplate<String, String> kafkaTemplate = new KafkaTemplate<> 
 (producerFactory());
   return kafkaTemplate;
 }

@Bean
public KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry() {
  KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry =
      new KafkaListenerEndpointRegistry();
  return kafkaListenerEndpointRegistry;
}

}

Place all the beans you need to include in the test in a different class:

@TestConfiguration
@Profile("kafka_test")
@EnableKafka
 public class KafkaBeansConfig {

 @Bean
  public MyProducer myProducer() {
  return new MyProducer();
 }

  // more beans
  }

I created a BaseKafkaConsumerTest class to reuse it :

 @ExtendWith(SpringExtension.class)
 @TestPropertySource(
     properties = {"spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}"})
@TestInstance(Lifecycle.PER_CLASS)
@DirtiesContext
@ContextConfiguration(classes = KafkaTestConfig.class)
@ActiveProfiles("kafka_test")
public class BaseKafkaConsumerTest {

    @Autowired
    protected EmbeddedKafkaBroker embeddedKafka;

    @Value("${spring.embedded.kafka.brokers}")
    private String brokerAddresses;

    @Autowired
    protected KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry;

    @Autowired
    protected KafkaTemplate<String, String> senderTemplate;

    public void setUp() {
        embeddedKafka.brokerProperty("controlled.shutdown.enable", true);

        for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
        .getListenerContainers()) {
        System.err.println(messageListenerContainer.getContainerProperties().toString());
        ContainerTestUtils.waitForAssignment(messageListenerContainer,
            embeddedKafka.getPartitionsPerTopic());
    }
    }

    @AfterAll
    public void tearDown() {
        for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
        .getListenerContainers()) {
        messageListenerContainer.stop();
    }

    embeddedKafka.getKafkaServers().forEach(b -> b.shutdown());
    embeddedKafka.getKafkaServers().forEach(b -> b.awaitShutdown());
    }

}

Extend the base class to stest your consumer:

@EmbeddedKafka(topics = MyConsumer.TOPIC_NAME)
@Import(KafkaBeansConfig.class)
public class MYKafkaConsumerTest extends BaseKafkaConsumerTest {

    private static Logger log = LoggerFactory.getLogger(PaymentMethodsKafkaConsumerTest.class);


    @Autowired
    private MyConsumer myConsumer;

        //mocks with @MockBean

    @Configuration
    @ComponentScan({"com.myfirm.kafka"})
    static class KafkaLocalTestConfig  {
    }

    @BeforeAll
    public void setUp() {
        super.setUp();
    }


    @Test
    public void testMessageIsReceived() throws Exception {

    //mocks

    String jsonPayload = "{\"id\":\"12345\","cookieDomain\":"helloworld"}";
    ListenableFuture<SendResult<String, String>> future =
        senderTemplate.send(MyConsumer.TOPIC_NAME, jsonPayload);

    Thread.sleep(10000);
    future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {

        @Override
    public void onSuccess(SendResult<String, String> result) {
        log.info("successfully sent message='{}' with offset={}", jsonPayload,
            result.getRecordMetadata().offset());
    }

    @Override
    public void onFailure(Throwable ex) {
        log.error("unable to send message='{}'", jsonPayload, ex);
    }
    });

    Mockito.verify(myService, Mockito.times(1))
    .update(Mockito.any(MyDetails.class));
    }

As I read in other posts, don´t test the business logic this way. Just that the calls are made.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!