'Test cases fails to load due to dependency injection failure in @Configuration class

I have a kafka config class with custom implementation and annotated with @Configuration. There is a method kafkaConsumer() annotated with @Bean and @Autowired and it returns an instance of ConsumerPropertiesBuilder. This method accepts fields - consumer builder, handler, topic, producer.

@Configuration
public class KafkaProperties {
    @Bean
    @Autowired
    public Consumer jobLegUpdateKafkaConsumer(ConsumerPropertiesBuilder consumer,
                                              Topic topic,
                                              Handler applicationEvenConsumer,
                                              Producer producer) {
        
        ........
        );
    }

}

Now I have junit classes setup. During initialization it fails with

Parameter 0 of method kafkaConsumer in com.abc.kafka.KafkaConsumerProperties required a bean of type 'com.abc.kafka.clients.properties.ConsumerProperties' that could not be found.

I have added a test class for kafka consumer but that is not helping either. How can I have the test method get injected instead of real beans being called during initialization? I'm returning a dummy instance for consumer for Mockito to consume instead of from actual @Configuration class

@Configuration
@Profile("test")
public class KafkaConsumerPropertiesTest {

@Bean
@Profile("test")
public Consumer testKafkaConsumer() {
    return new Consumer(new ConsumerProperties(), new Handler());
}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source