Compatibility with the latest Spring AMQP 2.0

Since Mockito cannot mock `final` methods, neither fields we get an `NPE` for the `final directReplyToContainers` in the `final RabbitTemplate.stop()`

* Change `mock()` to the `spy()` letting the real calls for the `final` methods.
* Fix `InboundEndpointTests` populating `Jackson2JsonMessageConverter` to the `RabbitTemplate`, because previously the stub method hasn't been called at all on the `mock` and we haven't known that there is no proper config for verification
This commit is contained in:
Artem Bilan
2016-12-30 09:59:43 -05:00
parent a0f0b6ab64
commit d973295631
3 changed files with 17 additions and 5 deletions

View File

@@ -301,7 +301,7 @@ public class AmqpOutboundChannelAdapterParserTests {
@Test
public void testInt3430FailForNotLazyConnect() {
RabbitTemplate amqpTemplate = mock(RabbitTemplate.class);
RabbitTemplate amqpTemplate = spy(new RabbitTemplate());
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
RuntimeException toBeThrown = new RuntimeException("Test Connection Exception");
doThrow(toBeThrown).when(connectionFactory).createConnection();

View File

@@ -22,6 +22,7 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import org.junit.After;
@@ -95,7 +96,7 @@ public class OutboundGatewayTests {
when(context.getBean(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME,
StandardEvaluationContext.class))
.thenReturn(evalContext);
RabbitTemplate template = mock(RabbitTemplate.class);
RabbitTemplate template = spy(new RabbitTemplate());
AmqpOutboundEndpoint endpoint = new AmqpOutboundEndpoint(template);
endpoint.setRoutingKeyExpression(PARSER.parseExpression("@foo"));
endpoint.setExchangeNameExpression(PARSER.parseExpression("@bar"));

View File

@@ -21,12 +21,17 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.mockito.Mockito;
@@ -163,7 +168,10 @@ public class InboundEndpointTests {
.build();
}));
RabbitTemplate rabbitTemplate = Mockito.mock(RabbitTemplate.class);
RabbitTemplate rabbitTemplate = spy(new RabbitTemplate());
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
CountDownLatch sendLatch = new CountDownLatch(1);
Mockito.doAnswer(invocation -> {
org.springframework.amqp.core.Message message =
@@ -176,9 +184,11 @@ public class InboundEndpointTests {
assertFalse(headers.containsKey(JsonHeaders.TYPE_ID));
assertFalse(headers.containsKey(JsonHeaders.KEY_TYPE_ID));
assertFalse(headers.containsKey(JsonHeaders.CONTENT_TYPE_ID));
sendLatch.countDown();
return null;
}).when(rabbitTemplate).send(Mockito.anyString(), Mockito.anyString(),
Mockito.any(org.springframework.amqp.core.Message.class), Mockito.any(CorrelationData.class));
}).when(rabbitTemplate)
.send(anyString(), anyString(), any(org.springframework.amqp.core.Message.class),
any(CorrelationData.class));
AmqpInboundGateway gateway = new AmqpInboundGateway(container, rabbitTemplate);
gateway.setMessageConverter(new Jackson2JsonMessageConverter());
@@ -198,6 +208,7 @@ public class InboundEndpointTests {
ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
listener.onMessage(amqpMessage, rabbitChannel);
assertTrue(sendLatch.await(10, TimeUnit.SECONDS));
}