Skip to content

Commit b7700ed

Browse files
committed
Improve reliability of kafka sample integration test
1 parent 91f7a2b commit b7700ed

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

spring-boot-samples/spring-boot-sample-kafka/src/test/java/sample/kafka/SampleKafkaApplicationTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
package sample.kafka;
1717

1818
import org.junit.jupiter.api.Test;
19-
import org.junit.jupiter.api.extension.RegisterExtension;
2019

20+
import org.springframework.beans.factory.annotation.Autowired;
2121
import org.springframework.boot.test.context.SpringBootTest;
22-
import org.springframework.boot.test.extension.OutputCapture;
2322
import org.springframework.kafka.test.context.EmbeddedKafka;
2423

2524
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,17 +35,18 @@
3635
@EmbeddedKafka(topics = "testTopic")
3736
class SampleKafkaApplicationTests {
3837

39-
@RegisterExtension
40-
OutputCapture output = new OutputCapture();
38+
@Autowired
39+
private Consumer consumer;
4140

4241
@Test
4342
void testVanillaExchange() throws Exception {
44-
long end = System.currentTimeMillis() + 30000;
45-
while (!this.output.toString().contains("A simple test message")
43+
long end = System.currentTimeMillis() + 10000;
44+
while (this.consumer.getMessages().isEmpty()
4645
&& System.currentTimeMillis() < end) {
4746
Thread.sleep(250);
4847
}
49-
assertThat(this.output).contains("A simple test message");
48+
assertThat(this.consumer.getMessages()).extracting("message")
49+
.containsOnly("A simple test message");
5050
}
5151

5252
}

0 commit comments

Comments
 (0)