模拟mvc检查会话中不存在该对象

时间:2015-11-11 20:07:16

标签: spring-mvc session spring-boot integration-testing mockmvc

我有以下测试

@Test
public void exitTest() throws Exception {
    mvc.perform(MockMvcRequestBuilders
            .get("/exit")
            .sessionAttr(CardController.CREDIT_CARD, mock(CreditCard.class))
            .accept(MediaType.TEXT_HTML))
            .andExpect(status().is(302))
            .andExpect(view().name("redirect:/")).andExpect(MockMvcResultMatchers.model().attributeDoesNotExist(CardController.CREDIT_CARD));

}

我想检查请求后会话中是否存在CardController.CREDIT_CARD

我的代码是否正确?

1 个答案:

答案 0 :(得分:2)

    .get("/exit")
            .sessionAttr(CardController.CREDIT_CARD, mock(CreditCard.class))
            .accept(MediaType.TEXT_HTML))
            .andExpect(status().is(302))
            .andExpect(view().name("redirect:/"))
            .andReturn()
            .getRequest()
            .getSession();

    assertNull(session.getAttribute(CardController.CREDIT_CARD));