1
1
/*
2
- * Copyright 2016-2022 the original author or authors.
2
+ * Copyright 2016-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -559,19 +559,21 @@ void testFutureFailureOnSend() {
559
559
560
560
@ SuppressWarnings ("unchecked" )
561
561
@ Test
562
- void testProducerInterceptorManagedOnKafkaTemplate () {
562
+ void testProducerInterceptorManagedOnKafkaTemplate () throws Exception {
563
563
564
564
Map <String , Object > senderProps = KafkaTestUtils .producerProps (embeddedKafka );
565
565
DefaultKafkaProducerFactory <Integer , String > pf = new DefaultKafkaProducerFactory <>(senderProps );
566
566
KafkaTemplate <Integer , String > template = new KafkaTemplate <>(pf , true );
567
567
ProducerInterceptor <Integer , String > producerInterceptor = Mockito .mock (ProducerInterceptor .class );
568
+ willAnswer (inv -> new ProducerRecord <>("prod-interceptor-test-1" , "bar" )).given (producerInterceptor ).onSend (any ());
568
569
template .setProducerInterceptor (producerInterceptor );
569
570
570
571
template .setDefaultTopic ("prod-interceptor-test-1" );
571
- template .sendDefault ("foo" );
572
+ CompletableFuture < SendResult < Integer , String >> resultCompletableFuture = template .sendDefault ("foo" );
572
573
573
574
verify (producerInterceptor , times (1 )).onSend (any (ProducerRecord .class ));
574
575
verify (producerInterceptor , times (1 )).onAcknowledgement (any (RecordMetadata .class ), Mockito .isNull ());
576
+ assertThat (resultCompletableFuture .get (10 , TimeUnit .SECONDS ).getProducerRecord ()).isEqualTo (new ProducerRecord <>("prod-interceptor-test-1" , "bar" ));
575
577
}
576
578
577
579
@ SuppressWarnings ("unchecked" )
@@ -591,13 +593,15 @@ void testProducerInterceptorNotSetOnKafkaTemplateNotInvoked() {
591
593
592
594
@ SuppressWarnings ("unchecked" )
593
595
@ Test
594
- void testCompositeProducerInterceptor () {
596
+ void testCompositeProducerInterceptor () throws Exception {
595
597
596
598
Map <String , Object > senderProps = KafkaTestUtils .producerProps (embeddedKafka );
597
599
DefaultKafkaProducerFactory <Integer , String > pf = new DefaultKafkaProducerFactory <>(senderProps );
598
600
KafkaTemplate <Integer , String > template = new KafkaTemplate <>(pf , true );
599
601
ProducerInterceptor <Integer , String > producerInterceptor1 = Mockito .mock (ProducerInterceptor .class );
600
602
ProducerInterceptor <Integer , String > producerInterceptor2 = Mockito .mock (ProducerInterceptor .class );
603
+ willAnswer (inv -> new ProducerRecord <>("prod-interceptor-test-3" , "bar" )).given (producerInterceptor1 ).onSend (any ());
604
+ willAnswer (inv -> new ProducerRecord <>("prod-interceptor-test-3" , "baz" )).given (producerInterceptor2 ).onSend (any ());
601
605
CompositeProducerInterceptor <Integer , String > compositeProducerInterceptor =
602
606
new CompositeProducerInterceptor <>(producerInterceptor1 , producerInterceptor2 );
603
607
template .setProducerInterceptor (compositeProducerInterceptor );
@@ -606,14 +610,15 @@ void testCompositeProducerInterceptor() {
606
610
doReturn (mockProducerRecord ).when (producerInterceptor1 ).onSend (any (ProducerRecord .class ));
607
611
608
612
template .setDefaultTopic ("prod-interceptor-test-3" );
609
- template .sendDefault ("foo" );
613
+ CompletableFuture < SendResult < Integer , String >> result = template .sendDefault ("foo" );
610
614
611
615
InOrder inOrder = inOrder (producerInterceptor1 , producerInterceptor2 );
612
616
613
617
inOrder .verify (producerInterceptor1 ).onSend (any (ProducerRecord .class ));
614
618
inOrder .verify (producerInterceptor2 ).onSend (any (ProducerRecord .class ));
615
619
inOrder .verify (producerInterceptor1 ).onAcknowledgement (any (RecordMetadata .class ), Mockito .isNull ());
616
620
inOrder .verify (producerInterceptor2 ).onAcknowledgement (any (RecordMetadata .class ), Mockito .isNull ());
621
+ assertThat (result .get (10 , TimeUnit .SECONDS ).getProducerRecord ()).isEqualTo (new ProducerRecord <>("prod-interceptor-test-3" , "baz" ));
617
622
}
618
623
619
624
@ ParameterizedTest (name = "{0} is invalid" )
0 commit comments