1212import org .slf4j .LoggerFactory ;
1313import org .springframework .beans .factory .annotation .Autowired ;
1414import org .springframework .boot .test .context .SpringBootTest ;
15+ import org .testcontainers .junit .jupiter .Container ;
16+ import org .testcontainers .junit .jupiter .Testcontainers ;
1517
1618import java .time .Duration ;
1719import java .util .List ;
2022import static org .junit .jupiter .api .Assertions .assertEquals ;
2123
2224@ SpringBootTest (classes = KafkaApplication .class )
23- public class MessagingFactoryTest {
25+ @ Testcontainers
26+ public final class MessagingFactoryTest {
2427
2528 final static Logger logger = LoggerFactory .getLogger (MessagingFactoryTest .class );
2629
30+ @ Container
31+ static ConfluentKafkaContainerCluster kafkaCluster = new ConfluentKafkaContainerCluster ("7.4.0" , 1 , 1 );
32+
2733 static class TestConsumer {
2834
2935 final AtomicInteger messageCount = new AtomicInteger ();
@@ -45,24 +51,25 @@ int getTotalMessages() {
4551 @ Autowired
4652 MessagingFactory messagingFactory ;
4753
48- ConfluentKafkaContainerCluster cluster ;
49-
5054 MessageProducer <TestMessage > processorOne ;
5155 MessageProducer <TestMessage > processorTwo ;
5256 MessageProducer <TestMessage > processorThree ;
5357
5458 @ BeforeEach
5559 void init () {
56- cluster = new ConfluentKafkaContainerCluster ("7.4.0" , 1 , 1 );
57- cluster .start ();
60+ kafkaCluster .start ();
61+ Awaitility .await ()
62+ .atMost (Duration .ofMinutes (1 ))
63+ .pollInterval (Duration .ofSeconds (5 ))
64+ .until (() -> kafkaCluster .getBrokers ().stream ().allMatch (b -> b .isRunning ()));
5865 processorOne = messagingFactory .createMessageProducer (Messaging .Topic .TOPIC_ONE , TestMessage .class );
5966 processorTwo = messagingFactory .createMessageProducer (Messaging .Topic .TOPIC_TWO , TestMessage .class );
6067 processorThree = messagingFactory .createMessageProducer (Messaging .Topic .TOPIC_THREE , TestMessage .class );
6168 }
6269
6370 @ AfterEach
6471 void destroy () {
65- cluster .stop ();
72+ kafkaCluster .stop ();
6673 }
6774
6875 @ Test
@@ -82,8 +89,8 @@ void test() throws InterruptedException {
8289 }
8390
8491 Awaitility .await ()
85- .atMost (Duration .ofSeconds (10 ))
86- .until (() -> testConsumer .getTotalMessages () = = 300 );
92+ .atMost (Duration .ofSeconds (20 ))
93+ .until (() -> testConsumer .getTotalMessages () > = 300 );
8794
8895 assertEquals (300 , testConsumer .getTotalMessages ());
8996 }
0 commit comments