Spring jmsTemplate 发送单元测试不起作用

Spring jmsTemplate send Unit testing doen't work

我的服务方法如下所示,我试图模拟JmsTemplate,以便它可以在单元测试期间发送消息,但它不执行jmsTemplate.send(...),直接进入下一步行,如何使用单元测试执行 jmsTemplate.send(..) 服务 class 的部分代码?

public int invokeCallbackListener(final MyObject payload, final MyTask task) throws Exception{
        //create map of payload and taskId
        int taskStatusCd = task.getTaskSatus().getCode();
        final Map<String, Object> map = new HashMap<String, Object>();
        map.put(PAYLOAD_KEY, payload);
        map.put(TASK_ID_KEY, task.getTaskId());

        //generate JMSCorrelationID
        final String correlationId = UUID.randomUUID().toString();

        String requestQueue = System.getProperty("REQUEST_QUEUE");
        requestQueue = requestQueue!=null?requestQueue:ExportConstants.DEFAULT_REQUEST_QUEUE;
        jmsTemplate.send(requestQueue, new MessageCreator() {           
            @Override
            public Message createMessage(Session session) throws JMSException {
                ***ObjectMessage message = session.createObjectMessage((Serializable)map)***; //fail here. Message returns null
                message.setJMSCorrelationID(correlationId);
                message.setStringProperty(MESSAGE_TYPE_PROPERTY,payload.getMessageType().getMessageType());             
                return message;
            }           
        });
        l.info("Request Message sent with correlationID: " + correlationId);

        taskStatusCd = waitForResponseStatus(task.TaskId(), taskStatusCd, correlationId);
        return taskStatusCd;
    }

这是我的测试class代码。

RemoteInvocationService remoteInvocationService;
    JmsTemplate mockTemplate;
    Session mockSession;
    Queue mockQueue;
    ObjectMessage mockMessage;
    MessageCreator mockmessageCreator;
    @Before
    public void setUp() throws Exception {
        remoteInvocationService = new RemoteInvocationService();
         mockTemplate = mock(JmsTemplate.class);
         mockSession = mock(Session.class);
         mockQueue = mock(Queue.class);
         mockMessage = mock(ObjectMessage.class);
         mockmessageCreator = mock(MessageCreator.class);
         when(mockSession.createObjectMessage()).thenReturn(mockMessage);
         when(mockQueue.toString()).thenReturn("testQueue");

         Mockito.doAnswer(new Answer<Message>() { 
             @Override 
             public Message answer(final InvocationOnMock invocation) throws JMSException { 
                final Object[] args = invocation.getArguments(); 
                   final String arg2  = (String)args[0];
            final MessageCreator arg = (MessageCreator)args[1];
                return arg.createMessage(mockSession); 
             } 
         }).when(mockTemplate).send(Mockito.any(MessageCreator.class)); 



            mockTemplate.setDefaultDestination(mockQueue);
            remoteInvocationService.setJmsTemplate(mockTemplate);
    }


    @Test
    public void testMessage() throws Exception{
        MyTask task = new MyTask();
        task.setTaskSatus(Status.Pending);

        remoteInvocationService.invokeCallbackListener(new MyObject(), task); 


    }

我有以下接收消息的代码,但是我得到的状态对象为空。

Message receivedMsg = jmsTemplate.receiveSelected(responseQueue, messageSelector);if(receivedMsg instanceof TextMessage){
                TextMessage status = (TextMessage) receivedMsg;             
                l.info(status.getText());}  

下面的测试代码:

  TextMessage mockTextMessage;
    when(mockSession.createTextMessage()).thenReturn(mockTextMessage);
    mockTextMessage.setText("5"); 

when(mockTemplate.receiveSelected(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(mockTextMessage)

你在模拟只接受一个参数的 send 方法 (MessageCreator),但你实际上调用的是接受两个参数 (String, MessageCreator) 的方法。

String 添加到您的模拟中:

     Mockito.doAnswer(new Answer<Message>() { 
         @Override 
         public Message answer(final InvocationOnMock invocation) throws JMSException { 
            final Object[] args = invocation.getArguments(); 
            final MessageCreator arg = (MessageCreator)args[0];
            return arg.createMessage(mockSession); 
         } 
     }).when(mockTemplate).send(Mockito.any(String.class), Mockito.any(MessageCreator.class));

模拟会话时还有一个错误。您正在模拟没有参数的方法:

when(mockSession.createObjectMessage()).thenReturn(mockMessage);

但您实际上需要使用 Serializable 参数模拟那个:

when(mockSession.createObjectMessage(Mockito.any(Serializable.class)).thenReturn(mockMessage);