如何使用 Mockito 模拟用户定义的注释

时间:2021-07-05 07:21:48

标签: spring-boot spring-mvc mockito junit4 junit5

我正在为我的控制器层编写测试用例。我的控制器方法之一如下所示。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/v1")
public class MyController {

  private static Logger logger = LoggerFactory.getLogger(MyController.class);

  @Autowired
  private MyService service;

  @GetMapping("/{pathVar}/check")
  @MyAuthentication
  public ResponseEntity<Object> check(@PathVariable String pathVar,
                                         @MyAuthContext MyAuthInfo myAuthInfo) throws Exception {
    try {
      logger.info("Check API started by "+myAuthInfo.getUserId()+" and email "+ myAuthInfo.getUserEmail());
      return ResponseEntity
          .status(HttpStatus.OK)
          .body(service.isPartner(pathVar, myAuthInfo));
    } catch (Exception e) {
      logger.info("Check API encountered the error.", e);
      throw e;
    }
  }
}

有人可以在我编写测试用例时帮助模拟我的身份验证上下文吗?

0 个答案:

没有答案
相关问题