From b7700ed094b6eec24d22bdb34958983c0e8e044d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 14 May 2019 09:40:46 +0200 Subject: [PATCH] Improve reliability of kafka sample integration test --- .../sample/kafka/SampleKafkaApplicationTests.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/spring-boot-samples/spring-boot-sample-kafka/src/test/java/sample/kafka/SampleKafkaApplicationTests.java b/spring-boot-samples/spring-boot-sample-kafka/src/test/java/sample/kafka/SampleKafkaApplicationTests.java index e74f623f691..06bb0eed8eb 100644 --- a/spring-boot-samples/spring-boot-sample-kafka/src/test/java/sample/kafka/SampleKafkaApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-kafka/src/test/java/sample/kafka/SampleKafkaApplicationTests.java @@ -16,10 +16,9 @@ package sample.kafka; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.extension.OutputCapture; import org.springframework.kafka.test.context.EmbeddedKafka; import static org.assertj.core.api.Assertions.assertThat; @@ -36,17 +35,18 @@ import static org.assertj.core.api.Assertions.assertThat; @EmbeddedKafka(topics = "testTopic") class SampleKafkaApplicationTests { - @RegisterExtension - OutputCapture output = new OutputCapture(); + @Autowired + private Consumer consumer; @Test void testVanillaExchange() throws Exception { - long end = System.currentTimeMillis() + 30000; - while (!this.output.toString().contains("A simple test message") + long end = System.currentTimeMillis() + 10000; + while (this.consumer.getMessages().isEmpty() && System.currentTimeMillis() < end) { Thread.sleep(250); } - assertThat(this.output).contains("A simple test message"); + assertThat(this.consumer.getMessages()).extracting("message") + .containsOnly("A simple test message"); } }