【发布时间】:2019-03-18 16:39:04
【问题描述】:
我在服务类中有一个方法,它使用外部包装器来调用 slack api。我正在使用的包装器是 one,如果它有什么不同的话。这就是我使用包装器的方式,
//This is the method in my service class
public String sendMess(SlackObj obj) {
//SlackObj contains the channel url, channel name and the message
//build the payload from the SlackObj
//Slack is the name of the wrapper's class that I'm using
Slack slack = Slack.getInstance();
//slack.send is the method that sends the message to slack
WebhookResponse res = slack.send(url, payload);
//other logic
}
//This is what I've tried
@Test
public void slackSendMessageTest(){
//build the slack obj and payload
//build the mock WebhookResponse
Slack slackMock = mock(Slack.class)
when(slackMock.send(channelUrl, payload)).thenReturn(mockWebHookRes);
assertEquals("SUCCESS", testService.sendMessage(testSlackObj);
}
我正在尝试为此方法编写一些测试,所以我的问题是,我如何在每次运行测试时不发送消息的情况下对其进行测试?我相信这是因为 slack 本身没有被模拟,我不知道如何将模拟注入到模拟的服务类中。
如果它有助于测试,我愿意重构服务类。任何建议和建议表示赞赏。谢谢。
【问题讨论】:
标签: java unit-testing spring-boot testing mockito