Commit b7700ed0 authored by Stephane Nicoll's avatar Stephane Nicoll

Improve reliability of kafka sample integration test

parent 91f7a2b7
...@@ -16,10 +16,9 @@ ...@@ -16,10 +16,9 @@
package sample.kafka; package sample.kafka;
import org.junit.jupiter.api.Test; 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.context.SpringBootTest;
import org.springframework.boot.test.extension.OutputCapture;
import org.springframework.kafka.test.context.EmbeddedKafka; import org.springframework.kafka.test.context.EmbeddedKafka;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -36,17 +35,18 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -36,17 +35,18 @@ import static org.assertj.core.api.Assertions.assertThat;
@EmbeddedKafka(topics = "testTopic") @EmbeddedKafka(topics = "testTopic")
class SampleKafkaApplicationTests { class SampleKafkaApplicationTests {
@RegisterExtension @Autowired
OutputCapture output = new OutputCapture(); private Consumer consumer;
@Test @Test
void testVanillaExchange() throws Exception { void testVanillaExchange() throws Exception {
long end = System.currentTimeMillis() + 30000; long end = System.currentTimeMillis() + 10000;
while (!this.output.toString().contains("A simple test message") while (this.consumer.getMessages().isEmpty()
&& System.currentTimeMillis() < end) { && System.currentTimeMillis() < end) {
Thread.sleep(250); Thread.sleep(250);
} }
assertThat(this.output).contains("A simple test message"); assertThat(this.consumer.getMessages()).extracting("message")
.containsOnly("A simple test message");
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment