有时候在被测试函数中,会调用其他的函数,对于UnitTest来说,如果该函数有返回值我们会像之前一样mock该函数,并返回我们想要的值,但对于没有返回值的函数来说,我们只需要知道是否被调用就可以了,现在我们想要在userId有异常时调用一次消息推送服务,让相关的人知道这里出了一个问题,消息推送服务如下
public interface IMessageService {
void tellSomeOneUserIdIsException(final Long userId);
}
@Service
public class MessageNotifyService implments IMessageService {
@Override
public void tellSomeOneUserIdIsException(final Long userId) {
//blablabla
}
}
消息推送的代码就不写了,然后在userCharacter中会调用消息推送
class FuncService {
@Autowired
private IMessageService messageService;
public boolean userCharacter(final Long userId) throws ServiceException {
// userId<0,我们调用消息推送
if (userId < 0) {
messageService.tellSomeOneUserIdIsException(userId);
throw new Exception(EXCEPTION_USER_ID_INVALID);