【发布时间】:2022-11-25 02:08:15
【问题描述】:
在 REST-Controller 上使用 @Secured 实现接口时,在 @WebMvcTest 中找不到控制器。删除 @Secured 注释或删除类上的实现将使其在测试中运行。
@Controller
@RequestMapping(path="/failing")
public class FailingTestController implements MyPasswordApi {
@RequestMapping(method = GET, produces = MediaType.APPLICATION_JSON_VALUE, path = "/test")
@Secured("ROLE_USER")
public ResponseEntity<GetEntity> getMethod()
和
@Controller
@RequestMapping(path = "/running")
public class RunningTestController {
@RequestMapping(method = GET, produces = MediaType.APPLICATION_JSON_VALUE, path = "/test")
@Secured("ROLE_USER")
public ResponseEntity<GetEntity> getMethod() {
都用于不同的 jUnit-5 测试。 “RunningTest”将成功(即 GET-Request 的状态为 200),而“FailingTest”将以状态 404 结束。使用注入的 RequestMapppingHanderMapping 可以看到,具有继承的控制器未绑定.
实际上,在应用程序中,找到了两个控制器。
我的问题是,如何测试实现安全性的控制器和一个界面。
在github上找到一个测试用例:https://github.com/sanddorn/Spring-Boot-Security-Rest-Showcase
【问题讨论】:
标签: spring-boot spring-security