Mock客户端的Mockito doThrow不会抛出异常

时间:2017-12-01 14:44:41

标签: jms mockito testcase

我正在为我的JMS客户端编写单元测试用例。但是doThrow并没有抛出任何错误。看起来是因为我的sendMessage方法的返回类型为void,我最后阻止关闭连接。

有人面临同样的问题吗?

doThrow(new JMSException("Expected")).when(messageSubmitter).sendMessage(message);

以下是sendMessage方法:

public void sendMessage(String message) throws JMSException,Exception {

    Connection connection = connectionFactory.createConnection();
    try {
         connection.start();
         try {
             Session session =  connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
             try {
                 MessageProducer messageProducer =session.createProducer(messageQueue);
                 try {
                         TextMessage message = session.createTextMessage();
                         message.setText(messageQueue);
                         messageProducer.send(message);
                     }
                 } finally {
                     JmsUtils.closeProducer(messageProducer);
                 }
             } finally {
                 JmsUtils.closeSession(session);
             }
         } finally {
             JmsUtils.closeConnection(connection);
         }
     } catch (JMSException ex) {
                  throw ex;
     } catch(Exception ex) {

          throw ex;
      }

}

这是我的测试方法,上面是我的jms客户端方法。

@Test 
public void myAction_should_return_failure_result_in_case_of_JMS_excepti‌​on() throws Exception { 
     // given 
     String message ="Test";     
     doThrow(exception).when(MessageSubmitter.sendMessage(message‌​);
     //when 
     ActionResult processingResult = Submitter.myActionOn("123"); 
     //then 
     assertFalse(processingResult.isProcessedSuccessfully()); 
     assertEquals(exception, processingResult.getException()); 
} 

0 个答案:

没有答案