integration test case for kafka

564 Views Asked by At

i am very new to junit test cases, i want to write junit for kafka integration. below is my code

@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());
    }
}

and in other class, i have one method where Messages will be sent to kafka by using kafkaTemplate

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());
            }

        });
}

now i want to write test case for KafkaConfig (@EnableKafka, @Configuration) and test case for public void sendDataToKafka(String topicName, String msg) method

i am very new for writing the junit test cases, can someone please help me on this

1

There are 1 best solutions below

3
On

What exactly would you like to test in the KafkaKonfig? IMHO this is one of the rare example where unit tests are redundant. You do thing but initializing beans there. So what value unit test brings you in this case?

Regarding pushing something to Kafka, I would say you would not do it with a unit test, you would rather write and integration test (IT). With unit test one tests the internal logic of a code unit. Meaning one do not care about the surroundings. Kafka in your case is an external thing so in a unit test you mock it instead of testing if a message is really pushed to Kafka. You might want to take a look at mockito framework for mocking (https://site.mockito.org) If I understand correctly not something you want to achieve in this particular case, but could be useful in general.

If you would like to test that messages are really pushed to Kafka, you actually want to write an IT not a unit test. The 'problematic' part of your IT is that you actually do not want to push messages to a shared Kafka instance, because your tests might interfere with actions other tests perform on the same Kafka instance(similar as if it were a DB). For example if you run a test on your machine and somebody else would run the same test on the same Kafka instance, your result might be compromised. So you might want to run an embedded Kafka instance for your ITs. (Again that would be exactly the same if your would like to test interactions with a DB, you want a separate instance of a DB for each test run) Once you use Spring you might want to look at the embedded Kafka that Spring framework introduces. You find a lot of examples on Internet For example: https://codenotfound.com/spring-kafka-embedded-unit-test-example.html

I believe you can have embedded Kafka instance without using a dependency to Spring. Just look for an example.