【发布时间】:2021-10-06 01:57:29
【问题描述】:
我有以下情况。
我想使用 mockito 来测试我的服务功能的 try/catch 块:GettingMyDataService。请问有什么提示或技巧吗?
//in the controller
@GetMapping("/data")
public ResponseEntity gettingMyDataController(@RequestParam(request = true) String dataId) {
gettingMyDataService.getDataFunction(dataId);
}
@Service
// my first service
public class GettingMyDataService {
@AutoWired
MyCustomRestClient myCustomRestClient
public void getDataFunction(String dataId) {
MyDataEntityDTO myDataEntityDTO;
try {
myDataEntityDTO = myCustomRestClient.callReadExternalSIService(dataId);
} catch (HttpStatusException e) {
if (e.getStatusCode().value() == 404) {
throw new CustomException1(".......");
}
else {
throw new CustomException2(".....");
}
}
}
@Service
// 2nd service for RestTemplate
public class myCustomRestClient {
public callReadExternalSIService(String dataId) {
String uri = "http:// ..."
HttpEntity<MyDataEntityDTO> res = RestTemplate.getForEntity(uri + dataId, MyDataEntityDTO.class);
return res.getBody();
}
}
【问题讨论】:
-
太棒了!谢谢你:)
标签: java spring-boot mockito try-catch resttemplate