我读过以下主题:
https://stackoverflow.com/a/18487953/2674303
但这有点不是我的变种。
在我需要测试的方法控制器中,我有以下行:
httpServletRequest.isUserInRole("ROLE_OWNER");
如何在MockHttpServletRequest
中设置角色?
答案 0 :(得分:1)
mockMvc.perform(post("/owner/terminals/edit").principal(principal).with(new RequestPostProcessor() {
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
request.addUserRole("ROLE_OWNER");
return request;
}