11package com .helltractor .demo .messaging ;
22
3- import com .helltractor .demo .ConfluentKafkaContainerCluster ;
3+ import com .helltractor .demo .ConfluentKafkaContainerStandalone ;
44import com .helltractor .demo .KafkaApplication ;
55import com .helltractor .demo .message .TestMessage ;
66import com .helltractor .demo .util .IpUtil ;
1111import org .slf4j .LoggerFactory ;
1212import org .springframework .beans .factory .annotation .Autowired ;
1313import org .springframework .boot .test .context .SpringBootTest ;
14+ import org .springframework .context .annotation .Lazy ;
15+ import org .springframework .test .annotation .DirtiesContext ;
1416
1517import java .util .List ;
1618import java .util .concurrent .atomic .AtomicInteger ;
1719
1820import static org .junit .jupiter .api .Assertions .assertEquals ;
1921
2022@ SpringBootTest (classes = KafkaApplication .class )
23+ @ DirtiesContext (classMode = DirtiesContext .ClassMode .AFTER_EACH_TEST_METHOD )
2124public class MessagingTest {
2225
2326 final static Logger logger = LoggerFactory .getLogger (MessagingTest .class );
2427
28+ @ Lazy
2529 @ Autowired
2630 MessagingFactory messagingFactory ;
2731
28- ConfluentKafkaContainerCluster cluster ;
32+ ConfluentKafkaContainerStandalone standalone ;
2933
3034 MessageProducer <TestMessage > processorOne ;
3135 MessageProducer <TestMessage > processorTwo ;
3236 MessageProducer <TestMessage > processorThree ;
3337
3438 @ BeforeEach
3539 void init () {
36- cluster = new ConfluentKafkaContainerCluster ("7.4.0" , 1 , 1 );
37- cluster .start ();
40+ standalone = new ConfluentKafkaContainerStandalone ("7.4.0" );
3841 processorOne = messagingFactory .createMessageProducer (Messaging .Topic .TOPIC_ONE , TestMessage .class );
3942 processorTwo = messagingFactory .createMessageProducer (Messaging .Topic .TOPIC_TWO , TestMessage .class );
4043 processorThree = messagingFactory .createMessageProducer (Messaging .Topic .TOPIC_THREE , TestMessage .class );
4144 }
4245
4346 @ AfterEach
4447 void destroy () {
45- cluster .stop ();
48+ standalone .stop ();
4649 }
4750
4851 static class TestConsumer {
@@ -65,7 +68,7 @@ int getTotalMessages() {
6568
6669 @ Test
6770 void test () throws InterruptedException {
68- for (int i = 0 ; i < 1000 ; i ++) {
71+ for (int i = 0 ; i < 100 ; i ++) {
6972 TestMessage testMessage = new TestMessage ();
7073 testMessage .message = "Test-" + i ;
7174 processorOne .sendMessages (testMessage );
@@ -80,6 +83,6 @@ void test() throws InterruptedException {
8083 }
8184
8285 Thread .sleep (10000 );
83- assertEquals (3000 , testConsumer .getTotalMessages ());
86+ assertEquals (300 , testConsumer .getTotalMessages ());
8487 }
8588}
0 commit comments