使用 Mockito 返回空连接测试 Solace Producer

Testing a Solace Producer With Mockito Returning a Null Connection

我正在尝试使用 Mockito 向 Solace 队列发送消息来测试以下 @Service。我可以为 SolConnectionFactory 注入我的 @Mock 并在模拟上调用 createQueueConnection() 但返回的模拟连接设置为空?我尝试从服务的开头删除 QueueConnection connection = null; 语句,但没有任何区别。这是安慰服务:

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

@Service
public class SolaceProducerImpl implements SolaceProducer {

    @Autowired
    private SolConnectionFactory solaceCachedConnectionFactory; 

    @Override
    public SolaceResponse sendMessage(Queue queue, String message) throws JMSException {

        log.info("Starting - solaceProducerImpl.sendMessageToSolace()");

        QueueConnection connection = null;
        QueueSession session = null;
        QueueSender sender = null;
        SolaceResponse solaceResponse = new SolaceResponse();

        try {
            connection = solaceCachedConnectionFactory.createQueueConnection();         
            session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);                   
            sender = session.createSender(queue);           
            TextMessage textMessage = session.createTextMessage();
            textMessage.setText(message);               
            sender.send(textMessage);
        } catch (Exception ex) {
            //logged exception
        } finally {
            if (sender != null) {
                sender.close();
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        log.info("Finished - solaceProducerImpl.sendMessageToSolace()");
        return solaceResponse;
    }
}

测试:

  import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.util.Assert;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import com.solacesystems.jms.SolConnectionFactory;


    @RunWith(MockitoJUnitRunner.class)
    public class SolaceProducerTest {   

        @Mock
        private SolConnectionFactory solaceCachedConnectionFactory; 

        @Mock
        private Queue queue;

        @Mock
        private QueueConnection connection; 

        @Mock
        private QueueSession queueSession;

        @Mock
        private QueueSender queueSender;

        @Mock
        private TextMessage textMessage;

        private static final String SAMPLE_TEXT = "Some Test Message";

        @InjectMocks
        private SolaceProducerImpl solaceProducer;

        @Test
        public void testSendMessage() throws JMSException { 
            Mockito.when(solaceCachedConnectionFactory.createConnection()).thenReturn(connection);
            Mockito.when(connection.createSession(false, Session.AUTO_ACKNOWLEDGE)).thenReturn(queueSession);
            Mockito.when(queueSession.createSender(queue)).thenReturn(queueSender);         
            textMessage.setText(SAMPLE_TEXT);
            Mockito.when(queueSession.createTextMessage()).thenReturn(textMessage);
            SolaceResponse result = solaceProducer.sendMessage(queue, SAMPLE_TEXT);
            Assert.notNull(result);
            Mockito.verify(solaceCachedConnectionFactory, Mockito.times(1)).createConnection();
            Mockito.verify(connection, Mockito.times(1)).createSession(false, Session.AUTO_ACKNOWLEDGE);
            Mockito.verify(connection, Mockito.times(1)).close();
            Mockito.verify(queueSession, Mockito.times(1)).createSender(queue);
            Mockito.verify(queueSession, Mockito.times(1)).close();
            Mockito.verify(queueSender, Mockito.times(1)).send(textMessage);
            Mockito.verify(queueSender, Mockito.times(1)).close();
        }

    }

问题似乎是 Mockito 没有模拟 solaceCachedConnectionFactory.createConnection(),导致它返回 null,尽管指示如此。

Mockito.when(solaceCachedConnectionFactory.createConnection()).thenReturn(connection);

我做了一个快速测试,并通过实例化被测试的 class 设法解决了这个问题。

@InjectMocks
private SolaceProducerImpl solaceProducer = new SolaceProducerImpl();

请注意,所提供的示例还有其他几个问题。 例如,示例错误地模拟了 connection.createSession 而不是 connection.createQueueSession

session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);                   

Mockito.when(connection.createSession(false, Session.AUTO_ACKNOWLEDGE)).thenReturn(queueSession);