@RestController @AllArgs Constructor@Slf4j public class Sms TemplateControllerimplements SmsTemplateApi private final SmsTemplateService smsTemplateService @Override @PcpsCommonLog(moduAttrbNo="01",functModuNo ="08",logToNo="0054", logOprTpNo="查询",loginfo="短信模板查询",remark="短信模板") public RestResponse<List<SmsTemplateDTO>>getAllTemplates(SmsTemplateQueryDTO smsTemplateQueryDTO){ try { List<SmsTemplateDTO> templateDTOS smsTemplateService.getAllTemplates(smsTemplateQueryDo getPushPlanTpCd()); return RestResponse.ok(templateDTos)} catch (Exception e) { log.error("查询失败:",e); return RestResponse.fail("查询失败"); @Override @PcpsCommonLog(moduAttrbNo="01",functModuNo="08", logToNo="0054",logOprTpNo="查询",loginfo="短信模板条件查询",remark="短信模板") public RestResponse<Pagelnfo<SmsTemplateDTO>> getTemplate(SmsTemplateQueryDTO smsTemplateQueryDTO){ try { Pageinfo<SmsTemplateDTO>templateDTOS=smsTemplateService.getTemplate(smsTemplateQueryDTO); return RestResponse.ok(templateDTOS)}catch(Exception e){ log.error("分页查询失败:",e); return RestResponse.fail('查询失败"); ) } @Override @PcpsCommonLog(moduAttrbNo ="01", functModuNo="08", ogTpNo= "0054", logOprTpNo = "新增”,!g!nfo="短信模板新增”,remark=“短信模板”) public RestResponse<string>addSmsTemplate(@RequestBody Sms TemplateDTOssTemplateDTO){ try { smsTemplateService.addTemplate(smsTemplateDTO) return RestResponse.ok("新增成功");} catch (Exception e) { log.error("新增失败:",e); return RestResponse.fail("新增失败"); @Override @PcpsCommonLog(moduAttrbNo="01",functModuNo="08",logTpNo="0054",logOprTpNo="修改",loginfo=“短信模板修改",remark="短信模板” public RestResponse<String>updateTemplate(SmsTemplateDTOsmsTemplateDTO){ try { smsTemplatyService.updateTemplate(smsTemplateDTO) return RestResponse.ok("修改成功"):}catch(BizException e){ return RestResponse.fall”该模板绑定了生效中或未生效的营销推送计划,不允许修改!”)] catch (Exception e){ log.error("修改失败:",e) return RestResponse.fal("修改失败”) 短信模板删除@Override @PcpsConmonLog(moduAttrbNo=“01”,functModuNo ="08",logTpNo="0054",logOprTpNo="删除",loginfo="短信模板删除",remark=“短信模板”) public RestResponse<string> deleteTemplatel@RequestBody Sms TemplateQueryDTO smsTemplateQueryDTO){ tryl smsTemplateService.deleteTemplate(smsTemplateQueryDTO.getTemplateld()); return RestResponse.ck(”删除成功”); } catch(BizException e){ return RestResponse.fal"该模板绑定了生效中或未生效的营销推送计划,不允许删除!");】 catch(Exceptione){ log.error(”删除失败:",e): return Rest Response.fail(“删除失败”): }}} 请根据以上代码编写单元测试,需要使用mockito.lenient().when(xx).thenReturn,不创建public void set()
时间: 2023-07-19 10:23:28 浏览: 142
以下是一个可能的测试代码示例,仅供参考:
```
@RunWith(MockitoJUnitRunner.class)
public class SmsTemplateControllerTest {
@InjectMocks
private SmsTemplateController smsTemplateController;
@Mock(lenient = true)
private SmsTemplateService smsTemplateService;
@Test
public void testGetAllTemplates() throws Exception {
// prepare
List<SmsTemplateDTO> templateDTOS = new ArrayList<>();
templateDTOS.add(new SmsTemplateDTO());
lenient().when(smsTemplateService.getAllTemplates(any(SmsTemplateQueryDTO.class))).thenReturn(templateDTOS);
// test
RestResponse<List<SmsTemplateDTO>> response = smsTemplateController.getAllTemplates(new SmsTemplateQueryDTO());
// verify
assertEquals(ResponseCode.SUCCESS.getCode(), response.getCode());
assertEquals(templateDTOS, response.getData());
}
@Test
public void testGetTemplate() throws Exception {
// prepare
PageInfo<SmsTemplateDTO> pageInfo = new PageInfo<>();
pageInfo.setList(new ArrayList<>());
lenient().when(smsTemplateService.getTemplate(any(SmsTemplateQueryDTO.class))).thenReturn(pageInfo);
// test
RestResponse<PageInfo<SmsTemplateDTO>> response = smsTemplateController.getTemplate(new SmsTemplateQueryDTO());
// verify
assertEquals(ResponseCode.SUCCESS.getCode(), response.getCode());
assertEquals(pageInfo, response.getData());
}
@Test
public void testAddSmsTemplate() throws Exception {
// prepare
SmsTemplateDTO smsTemplateDTO = new SmsTemplateDTO();
lenient().doNothing().when(smsTemplateService).addTemplate(smsTemplateDTO);
// test
RestResponse<String> response = smsTemplateController.addSmsTemplate(smsTemplateDTO);
// verify
assertEquals(ResponseCode.SUCCESS.getCode(), response.getCode());
assertEquals("新增成功", response.getData());
}
@Test
public void testUpdateTemplate() throws Exception {
// prepare
SmsTemplateDTO smsTemplateDTO = new SmsTemplateDTO();
lenient().doNothing().when(smsTemplateService).updateTemplate(smsTemplateDTO);
// test
RestResponse<String> response = smsTemplateController.updateTemplate(smsTemplateDTO);
// verify
assertEquals(ResponseCode.SUCCESS.getCode(), response.getCode());
assertEquals("修改成功", response.getData());
}
@Test
public void testDeleteTemplate() throws Exception {
// prepare
SmsTemplateQueryDTO smsTemplateQueryDTO = new SmsTemplateQueryDTO();
smsTemplateQueryDTO.setTemplateld("id");
lenient().doNothing().when(smsTemplateService).deleteTemplate("id");
// test
RestResponse<String> response = smsTemplateController.deleteTemplate(smsTemplateQueryDTO);
// verify
assertEquals(ResponseCode.SUCCESS.getCode(), response.getCode());
assertEquals("删除成功", response.getData());
}
}
```
阅读全文