0

I wanted to use embedded Kafka for a Spring boot application. I am able to use embedded Kafka for Junit testing, but while trying to use in main application, the embedded Kafka object is not being identified.

When trying to load Spring boot application the embedded kafka object is not being autowired. This is for NON testing flow.

@SpringBootApplication
@DirtiesContext
@EmbeddedKafka(topics = "TEST_TOPIC.P2.R2", partitions = 1, controlledShutdown = false, brokerProperties = {
        "listeners=PLAINTEXT://localhost:9092", "port=9092" })
public class MockKafkaProducerApplication {

    public static void main(String[] args) throws Exception {
        System.out.println("Starting Spring boot Application");
        SpringApplication.run(MockKafkaProducerApplication.class, args);

    }

}


@ActiveProfiles("kafka_test")
@Configuration
public class KafkaConsumerTestBase {
    private Logger LOGGER = LoggerFactory.getLogger(KafkaConsumerTestBase.class);

    @Autowired
    protected EmbeddedKafkaBroker embeddedKafka;


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

    @Autowired
    protected KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry;

    @Autowired
    protected KafkaTemplate<String, String> senderTemplate;

....... ........ }

Field embeddedKafka in com.dell.pde.kafka.KafkaConsumerTestBase required a bean of type 'org.springframework.kafka.test.EmbeddedKafkaBroker' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Manu
  • 1,379
  • 6
  • 24
  • 53

2 Answers2

2

Embedded Kafka is meant for testing, not for actual applications.

Annotation that can be specified on a test class that runs Spring Kafka based tests. Provides the following features over and above the regular Spring TestContext Framework:

...

A library that provides an in-memory Kafka instance to run your tests against.

If you want to make an actual mock-up application, you'll have to run an actual instance of Kafka too.

Quinten Scheppermans
  • 954
  • 1
  • 10
  • 29
1

@interface EmbeddedKafka its for testing purposes. If you inspect public class EmbeddedKafkaCondition you can see how spring test run it:

public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    Optional<AnnotatedElement> element = context.getElement();
    if (element.isPresent() && !this.springTestContext((AnnotatedElement)element.get())) {
        EmbeddedKafka embedded = (EmbeddedKafka)AnnotatedElementUtils.findMergedAnnotation((AnnotatedElement)element.get(), EmbeddedKafka.class);
        if (embedded != null) {
            EmbeddedKafkaBroker broker = this.getBrokerFromStore(context);
            if (broker == null) {
                broker = this.createBroker(embedded);
                BROKERS.set(broker);
                this.getStore(context).put("embedded-kafka", broker);
            }
        }
    }

    return ConditionEvaluationResult.enabled("");
}

private boolean springTestContext(AnnotatedElement annotatedElement) {
    return AnnotatedElementUtils.findAllMergedAnnotations(annotatedElement, ExtendWith.class).stream().filter((extended) -> {
        return Arrays.asList(extended.value()).contains(SpringExtension.class);
    }).findFirst().isPresent();
}

Try to overwrite this class to run it on your app.

I suggest you use docker to raise a kafka image directly.