I need to mock a RabbitMQ in my unit Test

后端 未结 3 2061
-上瘾入骨i
-上瘾入骨i 2021-02-07 19:17

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.

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-07 20:12

    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! :)

    • assumption for unit test:
      • RMQ is working fine and data send to it would be pushed in queue
      • Only thing to be tested is if the data generated is correct or not
      • and whether the call to RMQs 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);
            }
    

提交回复
热议问题