SpringMVC Controller单元测试静态引入通配符问题

来源:互联网 发布:商城系统 数据库设计 编辑:程序博客网 时间:2024/06/05 09:27

示例

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;import org.junit.Before;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.MediaType;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.MvcResult;import org.springframework.test.web.servlet.ResultActions;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:applicationContext.xml")@WebAppConfigurationpublic class Test {    @Autowired    private WebApplicationContext wac;    private MockMvc mockMvc;    @Before    public void setup() {        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();    }    @org.junit.Test    public void test() {        try {            ResultActions ra = mockMvc                    .perform(                            post("/printBill/print")                                    .accept(MediaType                                            .parseMediaType("application/json;charset=UTF-8"))                                    .param("printUrl", "aa"))                    .andExpect(status().isOk())                    .andExpect(                            content().contentType(                                    "application/json;charset=UTF-8"));            MvcResult result = ra.andReturn();            String response = result.getResponse().getContentAsString();            System.out.println(response);        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

在进行SpringMVC Controller单元测试的时候,一开始按正常引入,是无法获取get(),content()等方法。查阅源码:
部分源码

 <pre class="code"> * import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; * import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; * import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; * * // ... * * WebApplicationContext wac = ...; * * MockMvc mockMvc = webAppContextSetup(wac).build(); * * mockMvc.perform(get("/form")) *     .andExpect(status().isOk()) *     .andExpect(content().mimeType("text/html")) *     .andExpect(forwardedUrl("/WEB-INF/layouts/main.jsp")); * </pre>

需要静态引入MockMvcRequestBuilders.,MockMvcResultMatchers.,MockMvcBuilders.*
于是乎马上进行静态引入。发现使用通配符,就无法引入。查阅官方文档

Static Import The fluent API in the example above requires a few static imports such as MockMvcRequestBuilders., MockMvcResultMatchers., and MockMvcBuilders.. An easy way to find these classes is to search for types matching “MockMvc“. If using Eclipse, be sure to add them as “favorite static members” in the Eclipse preferences under Java → Editor → Content Assist → Favorites. That will allow use of content assist after typing the first character of the static method name. Other IDEs (e.g. IntelliJ) may not require any additional configuration. Just check the support for code completion on static members.

嗯,蹩脚的英文看着有点晕眩。但是仔细认真的猜测一下。如果通配符引入不进去,需要设置一下Java → Editor → Content Assist → Favorites。嗯,找到提示的位置。然后点击new Type→ Browse弹出搜索框。然后输入MockMvcRequestBuilders。选中OK。如此,再加入其它两个包。大功告成。

原创粉丝点击