Skip to content

Commit 510ee09

Browse files
committed
Fix KafkaProducerMessageHandlerTests for NPE on BeanFactory
* Migrate `KafkaDslTests` and `KafkaDslKotlinTests` to their own Embedded Kafka instead of global one. Looks like there is some race condition when single Kafka broker is running for the whole test suite.
1 parent f4b171d commit 510ee09

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

spring-integration-kafka/src/test/java/org/springframework/integration/kafka/dsl/KafkaDslTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import org.springframework.kafka.support.Acknowledgment;
7272
import org.springframework.kafka.support.DefaultKafkaHeaderMapper;
7373
import org.springframework.kafka.support.KafkaHeaders;
74+
import org.springframework.kafka.test.context.EmbeddedKafka;
7475
import org.springframework.kafka.test.utils.KafkaTestUtils;
7576
import org.springframework.messaging.Message;
7677
import org.springframework.messaging.MessageChannel;
@@ -97,6 +98,7 @@
9798
*/
9899
@SpringJUnitConfig
99100
@DirtiesContext
101+
@EmbeddedKafka
100102
public class KafkaDslTests {
101103

102104
private static final Log log = LogFactory.getLog(KafkaDslTests.class);
@@ -275,7 +277,7 @@ public static class ContextConfiguration {
275277

276278
private Object fromSource;
277279

278-
@Value("${spring.global.embedded.kafka.brokers}")
280+
@Value("${spring.kafka.bootstrap-servers}")
279281
String embeddedKafkaBrokers;
280282

281283
@Bean

spring-integration-kafka/src/test/java/org/springframework/integration/kafka/outbound/KafkaProducerMessageHandlerTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ void testTxNonTx() {
728728
given(template.getProducerFactory()).willReturn(mock(ProducerFactory.class));
729729
KafkaProducerMessageHandler<String, String> handler = new KafkaProducerMessageHandler<>(template);
730730
handler.setTopicExpression(new LiteralExpression("topic"));
731+
handler.setBeanFactory(mock());
731732
handler.handleMessage(new GenericMessage<>("foo"));
732733
verify(template, never()).executeInTransaction(any());
733734
verify(template).send(any(ProducerRecord.class));

spring-integration-kafka/src/test/kotlin/org/springframework/integration/kafka/dsl/kotlin/KafkaDslKotlinTests.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import org.springframework.kafka.requestreply.ReplyingKafkaTemplate
4747
import org.springframework.kafka.support.Acknowledgment
4848
import org.springframework.kafka.support.DefaultKafkaHeaderMapper
4949
import org.springframework.kafka.support.KafkaHeaders
50+
import org.springframework.kafka.test.context.EmbeddedKafka
5051
import org.springframework.kafka.test.utils.KafkaTestUtils
5152
import org.springframework.messaging.Message
5253
import org.springframework.messaging.MessageChannel
@@ -71,6 +72,7 @@ import java.util.stream.Stream
7172

7273
@SpringJUnitConfig
7374
@DirtiesContext
75+
@EmbeddedKafka
7476
class KafkaDslKotlinTests {
7577

7678
companion object {
@@ -205,7 +207,7 @@ class KafkaDslKotlinTests {
205207

206208
var fromSource: Any? = null
207209

208-
@Value("\${spring.global.embedded.kafka.brokers}")
210+
@Value("\${spring.kafka.bootstrap-servers}")
209211
lateinit var embeddedKafkaBrokers: String
210212

211213
@Bean

0 commit comments

Comments
 (0)