I am using a RabbitMQ in my project.
I have in my consumer the code of the client part of rabbitMQ and the connection need a tls1.1 to connect with the real MQ.
So here is how i did it, some stuffs might be here and there in process of hiding the necessary class implementation details, but you would get a hint! :)
send()
is happening or not! public class SomeClassTest {
private Config config;
private RmqConfig rmqConfig;
private static final ObjectMapper mapper = new ObjectMapper();
private JasperServerClient jasperServerClient;
// @Mock
@InjectMocks
private RabbitMQProducer rabbitMQProducer;
private Connection phoenixConnection;
private String targetNotificationMessage;
SomeClass someClassObject;
@Before
public void setUp() {
// Mock basic stuffs
config = mock(Config.class);
Connection = mock(Connection.class);
rabbitMQProducer = mock(RabbitMQProducer.class); // Imp
jasperServerClient = mock(JasperServerClient.class);
rmqConfig = RmqConfig.builder()
.host("localhost")
.port(5672)
.userName("guest")
.password("guest")
.queueName("somequeue_name")
.prefetch(1)
.build();
final String randomMessage = "This is a waste message";
Message mockMsg = Message.forSending(randomMessage.getBytes(), null, rmqConfig.getQueueName(), rmqConfig.getQueueName(), "text/plain", "UTF-8", true); // prepare a mock message
// Prepare service configs
ConnectionConfig connectionConfig = RmqConfigUtil.getConfig(rmqConfig);
ProducerConfig producerConfig = new ProducerConfigBuilder()
.exchange(rmqConfig.getQueueName())
.contentType("text/pain")
.contentEncoding("UTF-8")
.connection(connectionConfig).build();
rabbitMQProducer.open(croducerConfig.asMap());
// build the major stuff where the code resides
someClassObject = SomeClass.builder()
.phoenixConnection(phoenixConnection)
.userExchangeName(rmqConfig.getQueueName())
.userRabbitMQProducer(rabbitMQProducer)
.ftpConfig(config.getFtpConfig())
.jasperServerClient(jasperServerClient)
.objectMapper(new ObjectMapper())
.build();
MockitoAnnotations.initMocks(this);
}
@Test
public void testNotificationPub() throws Exception {
// Prepare expected Values
targetNotificationMessage = <>
// Reflection - my target functions were private
Class cls = Class.forName("com.some.path.to.class");
Object[] objForGetMessage = {<>, <>};
Method getNotificationMessage = cls.getDeclaredMethod("private_fn_1", <>.class, <>.class);
Method pubNotification = cls.getDeclaredMethod("private_fn_2", <>.class, RabbitMQProducer.class, String.class);
getNotificationMessage.setAccessible(true);
pubNotification.setAccessible(true);
// Test Case #1
final <> notificationMessage = (<>)getNotificationMessage.invoke(someClassObject, objForGetMessage);
assertEquals(notificationMessage.getMessage(), targetNotificationMessage);
// Test Case #2 - this does RMQ call
Object[] objPubMessage = {notificationMessage, rabbitMQProducer, rmqConfig.getQueueName()};
final Object publishNotification = pubNotification.invoke(someClassObject, objPubMessage);
assertEquals(publishNotificationResp, publishNotification); //viola
//Important, since RabbitMQProducer is mocked, we need to checkup if function call is made to "send" function which send data to RMQ
verify(rabbitMQProducer,times(1)).send(any());
}
@Test
public void testMockCreation(){
assertNotNull(rmqConfig);
assertNotNull(config);
}