Mockito 为拦截器模拟 HttpServletRequest

时间:2021-04-09 10:12:41

标签: java mockito spring-test

我必须检查是否调用了某些拦截器逻辑。 拦截器通过 request.getUserPrincipal 获取用户主体。所以我模拟了 HttpServletRequest 来提供一些自定义逻辑。 但是 request.getUserPrincipal 始终为 null,因为应用了 mockMvc 生成的数据而不是我模拟的 servletRequest

@WebMvcTest(value = MyApi.class)
public class UserDetailsInterceptorTest {
    private static final String USER = "User";
    private static final String ROLE = "Manager";

    @Autowired
    private MockMvc mockMvc;

    @Mock
    private HttpServletRequest request;

    private final String rootUrl = "/v1/my/info";
    
    @Test
    void interceptPositive() throws Exception {
        Principal principal = () -> USER;
        KeycloakAccount account = new SimpleKeycloakAccount(principal, Collections.singleton(ROLE), null);
        when(request.getUserPrincipal()).thenReturn(new KeycloakAuthenticationToken(account,false));
        mockMvc.perform(get(rootUrl)
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk());
        //check interceptors        
    }
}

@Component
public class UserDetailsInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(
            HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        AccessToken token = null;
        String tokenString  = null;
        Principal principal = request.getUserPrincipal(); //null despite beeing mocked
        //other code    
    }
}

模拟 HttpServletRequest 或为主体提供 Mockito 的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

我发现 mockMvc 实际上允许直接传递主体

mockMvc.perform(get(rootUrl)
        .principal(principal) //this way
        .contentType(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk());

不需要模拟 HttpServletRequest

相关问题