2015-11-11 60 views
0

不存在我有個下面的測試模擬MVC檢查對象在會話

@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
.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));