Skip to content

Commit 47b56fb

Browse files
garyrussellartembilan
authored andcommitted
GH-2456: Suppress Duplicate Annotations with Spy
Resolves #2456 When spying a `@RabbitListener` bean, duplicate methods are resolved as well as duplicate class level `@RabbitListener` annotations. **cherry-pick to 2.4.x** (will require instanceof polishing for Java 8)
1 parent e98ece5 commit 47b56fb

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/annotation/RabbitListenerAnnotationBeanPostProcessor.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -339,7 +339,8 @@ private TypeMetadata buildMetadata(Class<?> targetClass) {
339339
multiMethods.add(method);
340340
}
341341
}
342-
}, ReflectionUtils.USER_DECLARED_METHODS);
342+
}, ReflectionUtils.USER_DECLARED_METHODS
343+
.and(meth -> !meth.getDeclaringClass().getName().contains("$MockitoMock$")));
343344
if (methods.isEmpty() && multiMethods.isEmpty()) {
344345
return TypeMetadata.EMPTY;
345346
}
@@ -352,14 +353,25 @@ private TypeMetadata buildMetadata(Class<?> targetClass) {
352353
private List<RabbitListener> findListenerAnnotations(AnnotatedElement element) {
353354
return MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY)
354355
.stream(RabbitListener.class)
356+
.filter(tma -> {
357+
Object source = tma.getSource();
358+
String name = "";
359+
if (source instanceof Class<?>) {
360+
name = ((Class<?>) source).getName();
361+
}
362+
else if (source instanceof Method) {
363+
name = ((Method) source).getDeclaringClass().getName();
364+
}
365+
return !name.contains("$MockitoMock$");
366+
})
355367
.map(ann -> ann.synthesize())
356368
.collect(Collectors.toList());
357369
}
358370

359371
private void processMultiMethodListeners(RabbitListener[] classLevelListeners, Method[] multiMethods,
360372
Object bean, String beanName) {
361373

362-
List<Method> checkedMethods = new ArrayList<Method>();
374+
List<Method> checkedMethods = new ArrayList<>();
363375
Method defaultMethod = null;
364376
for (Method method : multiMethods) {
365377
Method checked = checkProxy(method, bean);

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/annotation/EnableRabbitIntegrationTests.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,12 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.fail;
2121
import static org.awaitility.Awaitility.await;
22+
import static org.mockito.ArgumentMatchers.any;
2223
import static org.mockito.BDDMockito.willAnswer;
2324
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.spy;
26+
import static org.mockito.Mockito.times;
27+
import static org.mockito.Mockito.verify;
2428

2529
import java.io.IOException;
2630
import java.io.Serializable;
@@ -385,6 +389,7 @@ public void multiListener() {
385389
rabbitTemplate.convertAndSend("multi.exch", "multi.rk", bar);
386390
assertThat(this.rabbitTemplate.receiveAndConvert("sendTo.replies"))
387391
.isEqualTo("CRASHCRASH Test reply from error handler");
392+
verify(this.multi, times(2)).bar(any());
388393
bar.field = "bar";
389394
Baz baz = new Baz();
390395
baz.field = "baz";
@@ -400,7 +405,7 @@ public void multiListener() {
400405
this.rabbitTemplate.setAfterReceivePostProcessors(mpp);
401406
assertThat(rabbitTemplate.convertSendAndReceive("multi.exch", "multi.rk", qux)).isEqualTo("QUX: qux: multi.rk");
402407
assertThat(beanMethodHeaders).hasSize(2);
403-
assertThat(beanMethodHeaders.get(0)).isEqualTo("MultiListenerBean");
408+
assertThat(beanMethodHeaders.get(0)).contains("$MultiListenerBean");
404409
assertThat(beanMethodHeaders.get(1)).isEqualTo("qux");
405410
this.rabbitTemplate.removeAfterReceivePostProcessor(mpp);
406411
assertThat(rabbitTemplate.convertSendAndReceive("multi.exch.tx", "multi.rk.tx", bar)).isEqualTo("BAR: barbar");
@@ -1966,7 +1971,7 @@ public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
19661971

19671972
@Bean
19681973
public MultiListenerBean multiListener() {
1969-
return new MultiListenerBean();
1974+
return spy(new MultiListenerBean());
19701975
}
19711976

19721977
@Bean

0 commit comments

Comments
 (0)