Skip to content
Permalink
Browse files

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**
  • Loading branch information
artembilan authored and garyrussell committed Apr 10, 2019
1 parent 9359d84 commit 1333fdabd2790c1e1b14afb4a38ed65168bf97d4
@@ -47,18 +47,20 @@
* by autowiring the test harness into test cases.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 1.6
*
*/
public class RabbitListenerTestHarness extends RabbitListenerAnnotationBeanPostProcessor {

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

private final Map<String, CaptureAdvice> listenerCapture = new HashMap<String, CaptureAdvice>();
private final Map<String, CaptureAdvice> listenerCapture = new HashMap<>();

private final AnnotationAttributes attributes;
private final Map<String, Object> listeners = new HashMap<>();

private final Map<String, Object> listeners = new HashMap<String, Object>();
private final AnnotationAttributes attributes;

public RabbitListenerTestHarness(AnnotationMetadata importMetadata) {
Map<String, Object> map = importMetadata.getAnnotationAttributes(RabbitListenerTest.class.getName());
@@ -111,33 +113,37 @@ public InvocationData getNextInvocationDataFor(String id, long wait, TimeUnit un
return (T) this.listeners.get(id);
}

@Override
public int getOrder() {
return super.getOrder() - 100;
}

private static final class CaptureAdvice implements MethodInterceptor {

private final BlockingQueue<InvocationData> invocationData = new LinkedBlockingQueue<InvocationData>();
private final BlockingQueue<InvocationData> invocationData = new LinkedBlockingQueue<>();

CaptureAdvice() {
super();
}

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object result = null;
boolean isListenerMethod =
AnnotationUtils.findAnnotation(invocation.getMethod(), RabbitListener.class) != null
|| AnnotationUtils.findAnnotation(invocation.getMethod(), RabbitHandler.class) != null;
try {
result = invocation.proceed();
Object result = invocation.proceed();
if (isListenerMethod) {
this.invocationData.put(new InvocationData(invocation, result));
}
return result;
}
catch (Throwable t) { // NOSONAR - rethrown below
if (isListenerMethod) {
this.invocationData.put(new InvocationData(invocation, t));
}
throw t;
}
return result;
}

}
@@ -31,6 +31,7 @@

import org.springframework.amqp.core.AnonymousQueue;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
@@ -50,6 +51,8 @@

/**
* @author Gary Russell
* @author Artem Bilan
*
* @since 1.6
*
*/
@@ -74,7 +77,7 @@
private RabbitListenerTestHarness harness;

@Test
public void testTwoWay() throws Exception {
public void testTwoWay() {
assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue1.getName(), "foo"));

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

@Configuration
@EnableRabbit
@RabbitListenerTest
public static class Config {

0 comments on commit 1333fda

Please sign in to comment.
You can’t perform that action at this time.