向Controller测试添加属性

时间:2018-01-25 11:18:26

标签: java spring

当使用@MockMvcTest测试控制器时,我们如何使用属性 - 示例.andExpect(model()。attributeExists(“classActiveLogin”))

我有这样的测试

@AutoConfigureMockMvc(secure = false)
@RunWith(SpringRunner.class)
@WebMvcTest({HomeController.class, ShoppingCartController.class})
public class ShoppingCartControllerTest {

    @MockBean
    private UserDetailsServiceImpl userDetailsService;

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private SecurityUtility securityUtility;

    @MockBean
    private BookService bookService;

    @MockBean
    private UserService userService;

    @MockBean
    private CartItemService cartItemService;

    @Test
    public void showLoginPage() throws Exception {

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

我得到了一个堆栈跟踪

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /login
       Parameters = {}
          Headers = {Content-Type=[text/html], Accept=[text/html]}

Handler:
             Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 404
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

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)
    at org.springframework.test.web.servlet.result.ModelResultMatchers.access$000(ModelResultMatchers.java:40)
    at org.springframework.test.web.servlet.result.ModelResultMatchers$3.match(ModelResultMatchers.java:84)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
    at com.valentine.bookstore.controller.ShoppingCartControllerTest.showLoginPage(ShoppingCartControllerTest.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)

这是我的方法

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

那么测试究竟出了什么问题呢?谢谢 我怀疑@WebMvcTest不会添加特定的上下文可能是域模型,有什么方法可以解决它?

我的控制器

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

1 个答案:

答案 0 :(得分:0)

您的状态404未找到,这意味着您的请求与控制器类中的任何请求映射都不匹配。

MockHttpServletResponse:
           Status = 404

您的控制器类是如何注释的?