我是Mockito Testing的新手
我想测试我的JMS客户端,在Jms客户端中有一个 void 方法
@Override
public void sendMessage(Serializable message) {
Connection conn = null;
Session session = null;
try {
conn = connectionFactory.createConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
Message msg = null;
if (message instanceof String) {
msg = session.createTextMessage();
((TextMessage) msg).setText((String) message);
} else {
msg = session.createObjectMessage();
((ObjectMessage) msg).setObject(message);
}
producer.send(msg);
if (log.isDebugEnabled()) {
log.debug("Successfully sent JMS message. Message: "
+ message.toString());
}
} catch (Exception e) {
log.error("Failed to send JMS message. ErrMsg: " + e.getMessage(), e);
} finally {
if (session != null) try { session.close(); } catch (Exception e) {}
if (conn != null) try { conn.close(); } catch (Exception e) {}
}
}
如何为像
这样的void方法创建模拟对象public void sendMessage(Serializable message)
我使用mock对象编写了void method()的测试用例是不是错了任何人请让我知道
public class SmsResourceTest {
private SmsResource smsResource;
private JmsClient jmsclient;
@Before
public void setUp()throws Exception{
smsResource = new SmsResource();
jmsclient = mock(JmsClientImpl.class);
}
@After
public void tearDown()throws Exception{
smsResource=null;
jmsclient=null;
}
@Test
public void testSendSms()throws Exception{
SmsNotificationDTO sms = new SmsNotificationDTO();
sms.setMessage("Sending Messages...");
try{
doNothing().doThrow(new RuntimeException())
.when(jmsclient).sendMessage(sms);
String output = sms.toString();
System.out.println(output);
smsResource.init();
Response res = smsResource.sendSms(null, sms);
Assert.assertEquals(200,res.getStatus());
System.out.println("response...... "+res.getStatus());
}catch(Exception e){
Assert.fail(e.getMessage());
}
}
}
请帮帮我......