Skip to content

Commit

Permalink
GH-2456: Suppress Duplicate Annotations with Spy
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
garyrussell authored and artembilan committed May 22, 2023
1 parent e98ece5 commit 47b56fb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -339,7 +339,8 @@ private TypeMetadata buildMetadata(Class<?> targetClass) {
multiMethods.add(method);
}
}
}, ReflectionUtils.USER_DECLARED_METHODS);
}, ReflectionUtils.USER_DECLARED_METHODS
.and(meth -> !meth.getDeclaringClass().getName().contains("$MockitoMock$")));
if (methods.isEmpty() && multiMethods.isEmpty()) {
return TypeMetadata.EMPTY;
}
Expand All @@ -352,14 +353,25 @@ private TypeMetadata buildMetadata(Class<?> targetClass) {
private List<RabbitListener> findListenerAnnotations(AnnotatedElement element) {
return MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY)
.stream(RabbitListener.class)
.filter(tma -> {
Object source = tma.getSource();
String name = "";
if (source instanceof Class<?>) {
name = ((Class<?>) source).getName();
}
else if (source instanceof Method) {
name = ((Method) source).getDeclaringClass().getName();
}
return !name.contains("$MockitoMock$");
})
.map(ann -> ann.synthesize())
.collect(Collectors.toList());
}

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

List<Method> checkedMethods = new ArrayList<Method>();
List<Method> checkedMethods = new ArrayList<>();
Method defaultMethod = null;
for (Method method : multiMethods) {
Method checked = checkProxy(method, bean);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,8 +19,12 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.io.IOException;
import java.io.Serializable;
Expand Down Expand Up @@ -385,6 +389,7 @@ public void multiListener() {
rabbitTemplate.convertAndSend("multi.exch", "multi.rk", bar);
assertThat(this.rabbitTemplate.receiveAndConvert("sendTo.replies"))
.isEqualTo("CRASHCRASH Test reply from error handler");
verify(this.multi, times(2)).bar(any());
bar.field = "bar";
Baz baz = new Baz();
baz.field = "baz";
Expand All @@ -400,7 +405,7 @@ public void multiListener() {
this.rabbitTemplate.setAfterReceivePostProcessors(mpp);
assertThat(rabbitTemplate.convertSendAndReceive("multi.exch", "multi.rk", qux)).isEqualTo("QUX: qux: multi.rk");
assertThat(beanMethodHeaders).hasSize(2);
assertThat(beanMethodHeaders.get(0)).isEqualTo("MultiListenerBean");
assertThat(beanMethodHeaders.get(0)).contains("$MultiListenerBean");
assertThat(beanMethodHeaders.get(1)).isEqualTo("qux");
this.rabbitTemplate.removeAfterReceivePostProcessor(mpp);
assertThat(rabbitTemplate.convertSendAndReceive("multi.exch.tx", "multi.rk.tx", bar)).isEqualTo("BAR: barbar");
Expand Down Expand Up @@ -1966,7 +1971,7 @@ public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {

@Bean
public MultiListenerBean multiListener() {
return new MultiListenerBean();
return spy(new MultiListenerBean());
}

@Bean
Expand Down

0 comments on commit 47b56fb

Please sign in to comment.