我尝试使用发送消息到Solace队列的@Service
来测试以下Mockito
。我可以为@Mock
注入SolConnectionFactory
并在Mock上调用createQueueConnection()
但是返回的Mock连接设置为null?我尝试从服务开始时删除QueueConnection connection = null;
语句,但它没有任何区别。这是Solace服务:
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();
}
}
答案 0 :(得分:2)
问题似乎是Mockito没有嘲笑solaceCachedConnectionFactory.createConnection()
,导致它返回null
,尽管有这样的指示。
Mockito.when(solaceCachedConnectionFactory.createConnection()).thenReturn(connection);
我做了一个快速测试,并设法通过实例化测试中的类来解决问题。
@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);