Skip to content

Commit

Permalink
GH-2943: EmbeddedKafka usage improvements in Kafka binder
Browse files Browse the repository at this point in the history
* Currently, EmbeddedKafka is initialized as part of the class initialization in a few tests
  in the Kafka binder. This is preventing individual JUnit tests from being executed from an IDE (IntelliJ, for example).
  If we move this initialization to the JUnit  method, then that seems to be working.

Resolves #2943
  • Loading branch information
sobychacko committed May 1, 2024
1 parent d534e84 commit c2754de
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2023 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.util.Collections;

import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
Expand All @@ -44,7 +45,12 @@
@EmbeddedKafka(brokerProperties = {"auto.create.topics.enable=false"})
class AutoCreateTopicDisabledTests {

private static final EmbeddedKafkaBroker embeddedKafka = EmbeddedKafkaCondition.getBroker();
private static EmbeddedKafkaBroker embeddedKafka;

@BeforeAll
public static void setUp() {
embeddedKafka = EmbeddedKafkaCondition.getBroker();
}

@Test
void autoCreateTopicDisabledFailsOnConsumerIfTopicNonExistentOnBroker() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ class KafkaBinderJaasInitializerListenerTest {

private static final String KAFKA_BROKERS_PROPERTY = "spring.cloud.stream.kafka.binder.brokers";

private static final EmbeddedKafkaBroker embeddedKafka = EmbeddedKafkaCondition.getBroker();

private static String JAVA_LOGIN_CONFIG_PARAM_VALUE;

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(KafkaBinderConfiguration.class, KafkaAutoConfiguration.class);

@BeforeAll
public static void setup() {
EmbeddedKafkaBroker embeddedKafka = EmbeddedKafkaCondition.getBroker();
System.setProperty(KAFKA_BROKERS_PROPERTY,
embeddedKafka.getBrokersAsString());
//Retrieve the current value for this system property if there is one set.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.springframework.cloud.stream.binder.kafka.bootstrap;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

Expand All @@ -32,11 +33,17 @@
*
* @author Marius Bogoevici
* @author Chris Bono
* @author Soby Chacko
*/
@EmbeddedKafka(controlledShutdown = true)
class KafkaBinderBootstrapTest {

private static final EmbeddedKafkaBroker embeddedKafka = EmbeddedKafkaCondition.getBroker();
private static EmbeddedKafkaBroker embeddedKafka;

@BeforeAll
public static void setUp() {
embeddedKafka = EmbeddedKafkaCondition.getBroker();
}

@ParameterizedTest
@ValueSource(booleans = { false, true })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"spring.cloud.stream.pollable-source=input"}
)
@DirtiesContext
@EmbeddedKafka(bootstrapServersProperty = "spring.kafka.bootstrap-servers")
@EmbeddedKafka
class KafkaBinderActuatorTests {

static final String TEST_CONSUMER_GROUP = "testGroup-actuatorTests";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2023 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -69,7 +69,7 @@
"spring.cloud.stream.kafka.default.consumer.ackEachRecord=true",
"spring.cloud.stream.kafka.bindings.custom-in.consumer.ackEachRecord=false" })
@DirtiesContext
@EmbeddedKafka(bootstrapServersProperty = "spring.kafka.bootstrap-servers")
@EmbeddedKafka
class KafkaBinderExtendedPropertiesTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2023 the original author or authors.
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,8 +42,7 @@
"spring.cloud.stream.bindings.kafka-binding-service-test.producer.partition-count=2",
"spring.cloud.stream.bindings.kafka-binding-service-test.producer.partition-key-expression=headers['partitionKey']"})
@DirtiesContext
@EmbeddedKafka(topics = "kafka-binding-service-test", controlledShutdown = true, partitions = 4,
bootstrapServersProperty = "spring.kafka.bootstrap-servers")
@EmbeddedKafka(topics = "kafka-binding-service-test", controlledShutdown = true, partitions = 4)
class KafkaBindingServiceTests {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 the original author or authors.
* Copyright 2016-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,7 +49,7 @@
"spring.kafka.consumer.auto-offset-reset=earliest",
"spring.cloud.stream.function.bindings.inputListen-in-0=kafkaNullInput"})
@DirtiesContext
@EmbeddedKafka(bootstrapServersProperty = "spring.kafka.bootstrap-servers")
@EmbeddedKafka
class KafkaNullConverterTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 the original author or authors.
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,7 +59,7 @@
"spring.cloud.stream.bindings.retryInContainer-in-0.group=bar",
"spring.cloud.stream.kafka.bindings.retryInBinder-in-0.consumer.enable-dlq=true",
"spring.cloud.stream.kafka.bindings.retryInContainer-in-0.consumer.enable-dlq=true"})
@EmbeddedKafka(bootstrapServersProperty = "spring.kafka.bootstrap-servers")
@EmbeddedKafka
@DirtiesContext
class KafkaRetryDlqBinderOrContainerTests {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -64,7 +64,7 @@
"spring.cloud.stream.kafka.binder.transaction.producer.configuration.acks=all"})
@DirtiesContext
@EmbeddedKafka(topics = "output", controlledShutdown = true, brokerProperties = {"transaction.state.log.replication.factor=1",
"transaction.state.log.min.isr=1"}, bootstrapServersProperty = "spring.kafka.bootstrap-servers")
"transaction.state.log.min.isr=1"})
class ProducerOnlyTransactionTests {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,6 +51,7 @@

/**
* @author Gary Russell
* @author Soby Chacko
* @since 3.0
*
*/
Expand All @@ -74,7 +75,7 @@
"spring.cloud.stream.kafka.binder.transaction.producer.configuration.acks=all"})
@DirtiesContext
@EmbeddedKafka(topics = "consumer.producer.txOut", controlledShutdown = true, brokerProperties = {"transaction.state.log.replication.factor=1",
"transaction.state.log.min.isr=1"}, bootstrapServersProperty = "spring.kafka.bootstrap-servers")
"transaction.state.log.min.isr=1"})
class ConsumerProducerTransactionTests {

@Autowired
Expand Down

0 comments on commit c2754de

Please sign in to comment.