@Override public boolean deleteTemplate(String tempateId) { List<PushPlanMsgDTO> pushPlanMsgDTos = pushPlanSetMapper.queryBindValidPushPlan(tempateId); if(!CollectionUtils.isEmpty(pushPlanMsgDTos)){ throw BizExeeption.error( warnningMessage:"该计划绑定了生效中的营销推送计划,不允许修改!");} smsTemplateMapper.deleteTemplate(tempateId); return true; } 根据以上代码生成单元测试案例,需要使用mockito.lenient().when(xx).thenReturn,目前已经写过的有@InjectMocks SmsTemplateServiceImpl smsTemplateService; @Mock SmsTemplateMapper smsTemplateMapper; @Mock PushPlanSetMapper pushPlanSetMapper;
时间: 2024-03-01 08:50:39 浏览: 122
C# 有关List<T>的Contains与Equals方法.doc
可以使用 `Mockito.doThrow` 来模拟抛出异常的情况,例如:
```
@Test
public void testDeleteTemplateThrowsException() {
// Mock dependencies
SmsTemplateMapper smsTemplateMapper = Mockito.mock(SmsTemplateMapper.class);
PushPlanSetMapper pushPlanSetMapper = Mockito.mock(PushPlanSetMapper.class);
List<PushPlanMsgDTO> pushPlanMsgDTos = new ArrayList<>();
pushPlanMsgDTos.add(new PushPlanMsgDTO());
Mockito.when(pushPlanSetMapper.queryBindValidPushPlan(Mockito.anyString())).thenReturn(pushPlanMsgDTos);
// Create the object under test
MyService myService = new MyService(smsTemplateMapper, pushPlanSetMapper);
// Invoke the method and expect an exception
assertThrows(BizExeeption.class, () -> myService.deleteTemplate("templateId"));
// Verify that the template was not deleted
Mockito.verify(smsTemplateMapper, Mockito.never()).deleteTemplate(Mockito.anyString());
}
```
这里模拟了 `queryBindValidPushPlan` 方法返回一个包含一个元素的列表,从而导致抛出业务异常。使用 `Mockito.never()` 来验证模板没有被删除。
阅读全文