【发布时间】:2020-05-22 03:12:08
【问题描述】:
我有一个这样的控制器:
@PostMapping
public ResponseEntity<RestResponse> test(@RequestBody @Valid RestRequest request) {
BodyRs BodyRs = service.setup(request.getBody());
return ResponseEntity.status(HttpStatus.OK).body(RestResponse.builder().body(BodyRs).build());
}
-
当我用 Request = '{}'(RestRequest not blank) 调用这个控制器时,它会跳转到:
public class RestExceptionHandler extends ResponseEntityExceptionHandler { @Autowired HolderService holderService; @Autowired LogService logService; @Override protected ResponseEntity<Object> handleNotValid(WebRequest request) { RestResponseHeader responseHeader = RestResponseHeader.builder() .respCode("10") .respDesc(fieldError.getField() + " " + fieldError.getDefaultMessage()) .build(); RestResponse restResponse = RestResponse.builder() .header(responseHeader) .build(); holderService.setRestResponse(restResponse); logService.log("100"); return ResponseEntity.ok().body(restResponse); } 如何模拟 LogService,因为当我在不启动 MQ 的情况下运行单元测试时,会在“logService.log("100")”这一行出错。
-我曾经在外面Mock过,但是当代码跳转到RestExceptionHandler时,它不再是一个Mock对象了。
【问题讨论】:
标签: java spring unit-testing