MockHttpServletRequest

MockHttpServletRequest

我已阅读以下主题:

https://stackoverflow.com/a/18487953/2674303

但这不是我的变体。

在我需要测试的方法控制器中,我有以下代码行:

httpServletRequest.isUserInRole("ROLE_OWNER");


如何在MockHttpServletRequest中设置角色?

最佳答案

 mockMvc.perform(post("/owner/terminals/edit").principal(principal).with(new RequestPostProcessor() {
            public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
                request.addUserRole("ROLE_OWNER");
                return request;
            }

07-25 22:34