Added backward compatibility with previous versions in terms of rabbitmq connection factory mocking

without this change we're not mocking all necessary methods in connection factory. When someone used the rabbittemplate, accidentaly we mocked the proper method to return a mocked connection factory. When rabbitmq started using different version of the `newConnection` in all places where rabbittemplate was used the real connection factory was created.
with this change we're mocking all `newConnection` methods to return a mocked connection

fixes #303
This commit is contained in:
Marcin Grzejszczak
2017-05-31 13:45:34 +02:00
parent 85a37345e3
commit 9fd4a21c43

View File

@@ -1,10 +1,11 @@
package org.springframework.cloud.contract.verifier.messaging.amqp;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.concurrent.ExecutorService;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -12,8 +13,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Spring rabbit test utility that provides a mock ConnectionFactory to avoid having to connect against a running broker.
@@ -30,13 +31,22 @@ public class RabbitMockConnectionFactoryAutoConfiguration {
@Bean
public ConnectionFactory connectionFactory() {
com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class);
Connection mockConnection = mock(Connection.class);
Channel mockChannel = mock(Channel.class);
final Connection mockConnection = mock(Connection.class);
final Channel mockChannel = mock(Channel.class);
com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class, new Answer() {
@Override public Object answer(InvocationOnMock invocationOnMock)
throws Throwable {
// hack for keeping backward compatibility with #303
if ("newConnection".equals(invocationOnMock.getMethod().getName())) {
return mockConnection;
}
return Mockito.RETURNS_DEFAULTS.answer(invocationOnMock);
}
});
try {
when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
when(mockConnection.isOpen()).thenReturn(true);
when(mockConnection.createChannel()).thenReturn(mockChannel);
when(mockConnection.createChannel(Mockito.anyInt())).thenReturn(mockChannel);
} catch (Exception e) {
throw new RuntimeException(e);
}