38
38
import org .apache .kafka .common .TopicPartition ;
39
39
import org .apache .kafka .common .header .Headers ;
40
40
import org .apache .kafka .common .header .internals .RecordHeaders ;
41
- import org .junit .jupiter .api .BeforeAll ;
42
41
import org .junit .jupiter .api .Test ;
43
42
44
43
import org .springframework .integration .IntegrationMessageHeaderAccessor ;
75
74
import org .springframework .kafka .support .converter .MessagingMessageConverter ;
76
75
import org .springframework .kafka .support .converter .RecordMessageConverter ;
77
76
import org .springframework .kafka .support .converter .StringJsonMessageConverter ;
77
+ import org .springframework .kafka .test .EmbeddedKafkaBroker ;
78
+ import org .springframework .kafka .test .context .EmbeddedKafka ;
78
79
import org .springframework .kafka .test .utils .ContainerTestUtils ;
79
80
import org .springframework .kafka .test .utils .KafkaTestUtils ;
80
81
import org .springframework .messaging .Message ;
111
112
* @since 5.4
112
113
*
113
114
*/
115
+ @ EmbeddedKafka (controlledShutdown = true ,
116
+ partitions = 1 ,
117
+ topics = {MessageDrivenAdapterTests .topic1 ,
118
+ MessageDrivenAdapterTests .topic2 ,
119
+ MessageDrivenAdapterTests .topic3 ,
120
+ MessageDrivenAdapterTests .topic4 ,
121
+ MessageDrivenAdapterTests .topic5 ,
122
+ MessageDrivenAdapterTests .topic6 })
114
123
class MessageDrivenAdapterTests implements TestApplicationContextAware {
115
124
116
125
static final String topic1 = "testTopic1" ;
@@ -125,16 +134,9 @@ class MessageDrivenAdapterTests implements TestApplicationContextAware {
125
134
126
135
static final String topic6 = "testTopic6" ;
127
136
128
- static String EMBEDDED_BROKERS ;
129
-
130
- @ BeforeAll
131
- static void setup () {
132
- EMBEDDED_BROKERS = System .getProperty ("spring.global.embedded.kafka.brokers" );
133
- }
134
-
135
137
@ Test
136
- void testInboundRecord () {
137
- Map <String , Object > props = KafkaTestUtils .consumerProps (EMBEDDED_BROKERS , "test1" , true );
138
+ void testInboundRecord (EmbeddedKafkaBroker embeddedKafka ) {
139
+ Map <String , Object > props = KafkaTestUtils .consumerProps (embeddedKafka , "test1" , true );
138
140
props .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "earliest" );
139
141
DefaultKafkaConsumerFactory <Integer , String > cf = new DefaultKafkaConsumerFactory <>(props );
140
142
ContainerProperties containerProps = new ContainerProperties (topic1 );
@@ -158,7 +160,7 @@ public Message<?> toMessage(ConsumerRecord<?, ?> record, Acknowledgment acknowle
158
160
adapter .start ();
159
161
ContainerTestUtils .waitForAssignment (container , 1 );
160
162
161
- Map <String , Object > senderProps = KafkaTestUtils .producerProps (EMBEDDED_BROKERS );
163
+ Map <String , Object > senderProps = KafkaTestUtils .producerProps (embeddedKafka );
162
164
ProducerFactory <Integer , String > pf = new DefaultKafkaProducerFactory <>(senderProps );
163
165
KafkaTemplate <Integer , String > template = new KafkaTemplate <>(pf );
164
166
template .setDefaultTopic (topic1 );
@@ -223,8 +225,8 @@ public Message<?> toMessage(ConsumerRecord<?, ?> record, Acknowledgment acknowle
223
225
}
224
226
225
227
@ Test
226
- void testInboundRecordRetryRecover () {
227
- Map <String , Object > props = KafkaTestUtils .consumerProps (EMBEDDED_BROKERS , "test4" , true );
228
+ void testInboundRecordRetryRecover (EmbeddedKafkaBroker embeddedKafka ) {
229
+ Map <String , Object > props = KafkaTestUtils .consumerProps (embeddedKafka , "test4" , true );
228
230
props .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "earliest" );
229
231
DefaultKafkaConsumerFactory <Integer , String > cf = new DefaultKafkaConsumerFactory <>(props );
230
232
ContainerProperties containerProps = new ContainerProperties (topic4 );
@@ -255,7 +257,7 @@ protected boolean doSend(Message<?> message, long timeout) {
255
257
adapter .start ();
256
258
ContainerTestUtils .waitForAssignment (container , 1 );
257
259
258
- Map <String , Object > senderProps = KafkaTestUtils .producerProps (EMBEDDED_BROKERS );
260
+ Map <String , Object > senderProps = KafkaTestUtils .producerProps (embeddedKafka );
259
261
ProducerFactory <Integer , String > pf = new DefaultKafkaProducerFactory <>(senderProps );
260
262
KafkaTemplate <Integer , String > template = new KafkaTemplate <>(pf );
261
263
template .setDefaultTopic (topic4 );
@@ -291,8 +293,8 @@ protected boolean doSend(Message<?> message, long timeout) {
291
293
* to the consumer.
292
294
*/
293
295
@ Test
294
- void testInboundRecordRetryRecoverWithoutRecoveryCallback () throws Exception {
295
- Map <String , Object > props = KafkaTestUtils .consumerProps (EMBEDDED_BROKERS , "test6" , true );
296
+ void testInboundRecordRetryRecoverWithoutRecoveryCallback (EmbeddedKafkaBroker embeddedKafka ) throws Exception {
297
+ Map <String , Object > props = KafkaTestUtils .consumerProps (embeddedKafka , "test6" , true );
296
298
props .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "earliest" );
297
299
DefaultKafkaConsumerFactory <Integer , String > cf = new DefaultKafkaConsumerFactory <>(props );
298
300
ContainerProperties containerProps = new ContainerProperties (topic6 );
@@ -329,7 +331,7 @@ public <T, E extends Throwable> void onError(RetryContext context, RetryCallback
329
331
adapter .start ();
330
332
ContainerTestUtils .waitForAssignment (container , 1 );
331
333
332
- Map <String , Object > senderProps = KafkaTestUtils .producerProps (EMBEDDED_BROKERS );
334
+ Map <String , Object > senderProps = KafkaTestUtils .producerProps (embeddedKafka );
333
335
DefaultKafkaProducerFactory <Integer , String > pf = new DefaultKafkaProducerFactory <>(senderProps );
334
336
KafkaTemplate <Integer , String > template = new KafkaTemplate <>(pf );
335
337
template .setDefaultTopic (topic6 );
@@ -342,8 +344,8 @@ public <T, E extends Throwable> void onError(RetryContext context, RetryCallback
342
344
}
343
345
344
346
@ Test
345
- void testInboundRecordNoRetryRecover () {
346
- Map <String , Object > props = KafkaTestUtils .consumerProps (EMBEDDED_BROKERS , "test5" , true );
347
+ void testInboundRecordNoRetryRecover (EmbeddedKafkaBroker embeddedKafka ) {
348
+ Map <String , Object > props = KafkaTestUtils .consumerProps (embeddedKafka , "test5" , true );
347
349
props .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "earliest" );
348
350
DefaultKafkaConsumerFactory <Integer , String > cf = new DefaultKafkaConsumerFactory <>(props );
349
351
ContainerProperties containerProps = new ContainerProperties (topic5 );
@@ -371,7 +373,7 @@ protected boolean doSend(Message<?> message, long timeout) {
371
373
adapter .start ();
372
374
ContainerTestUtils .waitForAssignment (container , 1 );
373
375
374
- Map <String , Object > senderProps = KafkaTestUtils .producerProps (EMBEDDED_BROKERS );
376
+ Map <String , Object > senderProps = KafkaTestUtils .producerProps (embeddedKafka );
375
377
ProducerFactory <Integer , String > pf = new DefaultKafkaProducerFactory <>(senderProps );
376
378
KafkaTemplate <Integer , String > template = new KafkaTemplate <>(pf );
377
379
template .setDefaultTopic (topic5 );
@@ -399,8 +401,8 @@ protected boolean doSend(Message<?> message, long timeout) {
399
401
}
400
402
401
403
@ Test
402
- void testInboundBatch () throws Exception {
403
- Map <String , Object > props = KafkaTestUtils .consumerProps (EMBEDDED_BROKERS , "test2" , true );
404
+ void testInboundBatch (EmbeddedKafkaBroker embeddedKafka ) throws Exception {
405
+ Map <String , Object > props = KafkaTestUtils .consumerProps (embeddedKafka , "test2" , true );
404
406
props .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "earliest" );
405
407
DefaultKafkaConsumerFactory <Integer , String > cf = new DefaultKafkaConsumerFactory <>(props );
406
408
ContainerProperties containerProps = new ContainerProperties (topic2 );
@@ -432,7 +434,7 @@ public Message<?> toMessage(List<ConsumerRecord<?, ?>> records, Acknowledgment a
432
434
adapter .start ();
433
435
ContainerTestUtils .waitForAssignment (container , 1 );
434
436
435
- Map <String , Object > senderProps = KafkaTestUtils .producerProps (EMBEDDED_BROKERS );
437
+ Map <String , Object > senderProps = KafkaTestUtils .producerProps (embeddedKafka );
436
438
ProducerFactory <Integer , String > pf = new DefaultKafkaProducerFactory <>(senderProps );
437
439
KafkaTemplate <Integer , String > template = new KafkaTemplate <>(pf );
438
440
template .setDefaultTopic (topic2 );
@@ -490,8 +492,8 @@ public Message<?> toMessage(List<ConsumerRecord<?, ?>> records, Acknowledgment a
490
492
}
491
493
492
494
@ Test
493
- void testInboundJson () {
494
- Map <String , Object > props = KafkaTestUtils .consumerProps (EMBEDDED_BROKERS , "test3" , true );
495
+ void testInboundJson (EmbeddedKafkaBroker embeddedKafka ) {
496
+ Map <String , Object > props = KafkaTestUtils .consumerProps (embeddedKafka , "test3" , true );
495
497
props .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "earliest" );
496
498
DefaultKafkaConsumerFactory <Integer , String > cf = new DefaultKafkaConsumerFactory <>(props );
497
499
ContainerProperties containerProps = new ContainerProperties (topic3 );
@@ -506,7 +508,7 @@ void testInboundJson() {
506
508
adapter .start ();
507
509
ContainerTestUtils .waitForAssignment (container , 1 );
508
510
509
- Map <String , Object > senderProps = KafkaTestUtils .producerProps (EMBEDDED_BROKERS );
511
+ Map <String , Object > senderProps = KafkaTestUtils .producerProps (embeddedKafka );
510
512
ProducerFactory <Integer , String > pf = new DefaultKafkaProducerFactory <>(senderProps );
511
513
KafkaTemplate <Integer , String > template = new KafkaTemplate <>(pf );
512
514
template .setDefaultTopic (topic3 );
@@ -534,8 +536,8 @@ void testInboundJson() {
534
536
}
535
537
536
538
@ Test
537
- void testInboundJsonWithPayload () {
538
- Map <String , Object > props = KafkaTestUtils .consumerProps (EMBEDDED_BROKERS , "test7" , true );
539
+ void testInboundJsonWithPayload (EmbeddedKafkaBroker embeddedKafka ) {
540
+ Map <String , Object > props = KafkaTestUtils .consumerProps (embeddedKafka , "test7" , true );
539
541
props .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "earliest" );
540
542
DefaultKafkaConsumerFactory <Integer , Foo > cf = new DefaultKafkaConsumerFactory <>(props );
541
543
ContainerProperties containerProps = new ContainerProperties (topic6 );
@@ -554,7 +556,7 @@ void testInboundJsonWithPayload() {
554
556
adapter .start ();
555
557
ContainerTestUtils .waitForAssignment (container , 1 );
556
558
557
- Map <String , Object > senderProps = KafkaTestUtils .producerProps (EMBEDDED_BROKERS );
559
+ Map <String , Object > senderProps = KafkaTestUtils .producerProps (embeddedKafka );
558
560
ProducerFactory <Integer , String > pf = new DefaultKafkaProducerFactory <>(senderProps );
559
561
KafkaTemplate <Integer , String > template = new KafkaTemplate <>(pf );
560
562
template .setDefaultTopic (topic6 );
0 commit comments