在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请求将具有状态200),而"FailingTest”将以状态404结束。使用注入的RequestMapppingHanderMapping可以看到,带有继承的控制器不受约束。
实际上,在应用程序中,可以找到两个控制器。
我的问题是,如何测试实现安全性和接口的控制器。
在github:https://github.com/sanddorn/Spring-Boot-Security-Rest-Showcase上找到了一个测试案例
发布于 2022-11-25 08:30:40
真正的答案只能在github(见问题)中找到。根本问题是用@SpringBootApplication 和 @EnableGlobalMethodSecurity来注释应用程序类
@SpringBootApplication
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class TestApplication {
public static void main(String []argv) {
SpringApplication.run(TestApplication.class);
}
}使用不同的配置类,如
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class TestConfiguration {
}并删除对应用程序类的所有额外注释解决了问题。
发布于 2022-11-24 18:08:15
您使用了错误的注释,请将Controller替换为RestController
@RestController
@RequestMapping(path = "/running")
public class RunningTestController {
}https://stackoverflow.com/questions/74564691
复制相似问题