Skip to content

Commit ff49223

Browse files
artembilangaryrussell
authored andcommitted
GH-973: Higher order for RabbitListenerTestHarness
Fixes #973 When we use `@EnableRabbit` and `@RabbitListenerTest` in the same configuration set, e.g. mixing real `@Configuration` and test one for `@RabbitListenerTest`, we may end up with the case when `@EnableRabbit` is processed before `@RabbitListenerTest`, so, `RabbitListenerTestHarness` bean is not going to appear in the application context. * Override `getOrder()` for the `RabbitListenerTestHarness` to give it higher priority than regular `RabbitListenerAnnotationBeanPostProcessor`, so it is registered first and then the last one won't override existing bean **Cherry-pick to 2.1.x**
1 parent 45ec57f commit ff49223

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

spring-rabbit-test/src/main/java/org/springframework/amqp/rabbit/test/RabbitListenerTestHarness.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,20 @@
4747
* by autowiring the test harness into test cases.
4848
*
4949
* @author Gary Russell
50+
* @author Artem Bilan
51+
*
5052
* @since 1.6
5153
*
5254
*/
5355
public class RabbitListenerTestHarness extends RabbitListenerAnnotationBeanPostProcessor {
5456

5557
private final Log logger = LogFactory.getLog(this.getClass());
5658

57-
private final Map<String, CaptureAdvice> listenerCapture = new HashMap<String, CaptureAdvice>();
59+
private final Map<String, CaptureAdvice> listenerCapture = new HashMap<>();
5860

59-
private final AnnotationAttributes attributes;
61+
private final Map<String, Object> listeners = new HashMap<>();
6062

61-
private final Map<String, Object> listeners = new HashMap<String, Object>();
63+
private final AnnotationAttributes attributes;
6264

6365
public RabbitListenerTestHarness(AnnotationMetadata importMetadata) {
6466
Map<String, Object> map = importMetadata.getAnnotationAttributes(RabbitListenerTest.class.getName());
@@ -112,33 +114,37 @@ public <T> T getSpy(String id) {
112114
return (T) this.listeners.get(id);
113115
}
114116

117+
@Override
118+
public int getOrder() {
119+
return super.getOrder() - 100;
120+
}
121+
115122
private static final class CaptureAdvice implements MethodInterceptor {
116123

117-
private final BlockingQueue<InvocationData> invocationData = new LinkedBlockingQueue<InvocationData>();
124+
private final BlockingQueue<InvocationData> invocationData = new LinkedBlockingQueue<>();
118125

119126
CaptureAdvice() {
120127
super();
121128
}
122129

123130
@Override
124131
public Object invoke(MethodInvocation invocation) throws Throwable {
125-
Object result = null;
126132
boolean isListenerMethod =
127133
AnnotationUtils.findAnnotation(invocation.getMethod(), RabbitListener.class) != null
128134
|| AnnotationUtils.findAnnotation(invocation.getMethod(), RabbitHandler.class) != null;
129135
try {
130-
result = invocation.proceed();
136+
Object result = invocation.proceed();
131137
if (isListenerMethod) {
132138
this.invocationData.put(new InvocationData(invocation, result));
133139
}
140+
return result;
134141
}
135142
catch (Throwable t) { // NOSONAR - rethrown below
136143
if (isListenerMethod) {
137144
this.invocationData.put(new InvocationData(invocation, t));
138145
}
139146
throw t;
140147
}
141-
return result;
142148
}
143149

144150
}

spring-rabbit-test/src/test/java/org/springframework/amqp/rabbit/test/ExampleRabbitListenerSpyTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import org.springframework.amqp.core.AnonymousQueue;
3333
import org.springframework.amqp.core.Queue;
34+
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
3435
import org.springframework.amqp.rabbit.annotation.RabbitListener;
3536
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
3637
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
@@ -50,6 +51,8 @@
5051

5152
/**
5253
* @author Gary Russell
54+
* @author Artem Bilan
55+
*
5356
* @since 1.6
5457
*
5558
*/
@@ -74,7 +77,7 @@ public class ExampleRabbitListenerSpyTest {
7477
private RabbitListenerTestHarness harness;
7578

7679
@Test
77-
public void testTwoWay() throws Exception {
80+
public void testTwoWay() {
7881
assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue1.getName(), "foo"));
7982

8083
Listener listener = this.harness.getSpy("foo");
@@ -99,6 +102,7 @@ public void testOneWay() throws Exception {
99102
}
100103

101104
@Configuration
105+
@EnableRabbit
102106
@RabbitListenerTest
103107
public static class Config {
104108

0 commit comments

Comments
 (0)