我正在尝试使用 spring boot 学习 junit 测试。通常我不会只问空指针异常,但现在我找不到丢失的东西。
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@ActiveProfiles("dev")
@AutoConfigureMockMvc
@WithMockUser(username = "user", password = "secret", authorities = "USER")
public class OwnersWebMVCTests {
@Autowired
private MockMvc mockMvc;
@Test
public void testOwners() throws Exception {
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/owners");
ResultActions resultActions = mockMvc.perform(requestBuilder);
MvcResult mvcResult = resultActions.andReturn();
ModelAndView mav = mvcResult.getModelAndView();
MatcherAssert.assertThat(mav.getViewName(), Matchers.equalTo("owners"));
MatcherAssert.assertThat(mav.getModel().containsKey("owners"), Matchers.is(true));
}
}
ModelAndView mav = mvcResult.getModelAndView(); 返回 null。
慕少森
相关分类