andExpect metodu - ResultMatcher
ResultMatcher arayüzünü gerçekleştiren tek sınıf MockMvcResultMatchers
Örnek
Şöyle yaparız.
@Mock
SecurityContext context;
@Mock
Authentication auth;
@Mock
Principal principal;
@Test
public void verifyCustomerInfoUnauthorized () throws Exception
{
when(context.getAuthentication()).thenReturn(auth);
when(context.getAuthentication().getPrincipal()).thenReturn(principal);
SecurityContextHolder.setContext(context);
mockMvc.perform(
MockMvcRequestBuilders.post("/customer")
.principal()
.contentType(MediaType.APPLICATION_JSON)
.content("{}")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized()).andExpect(status().is(401));
}
Hiç yorum yok:
Yorum Gönder