mvc测试中的断言错误

时间:2018-01-26 09:58:58

标签: spring error-handling

有一个类,并且不断获得任何模型和视图

@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
@WebMvcTest(value = HomeController.class, secure = false)
@ContextConfiguration( classes = {SecurityConfig.class})
@EntityScan(basePackages = {"com.valentine.bookstore", "com.valentine.service", "com.valentine.repository"})
public class HomeControllerTest {


 @Test
        public void showLoginPage() throws Exception {

            mockMvc.perform(get("/login")
                    .accept(MediaType.TEXT_HTML)
                    .contentType(MediaType.TEXT_HTML)
            )
                    .andExpect(model().attributeExists("classActiveLogin"))
                    .andReturn();
        }

控制器

@RequestMapping("/login")
public String login(Model model) {
    model.addAttribute("classActiveLogin", true);
    return "Myaccount";
}


java.lang.AssertionError: No ModelAndView found

    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:35)
    at org.springframework.test.util.AssertionErrors.assertTrue(AssertionErrors.java:65)
    at org.springframework.test.web.servlet.result.ModelResultMatchers.getModelAndView(ModelResultMatchers.java:272)

我不知道测试会出现什么问题,当我使用@springboottest时它会通过

0 个答案:

没有答案