SpringMVC Controller单元测试静态引入通配符问题
2017-06-19 10:51
435 查看
示例
在进行SpringMVC Controller单元测试的时候,一开始按正常引入,是无法获取get(),content()等方法。查阅源码:
部分源码
需要静态引入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。如此,再加入其它两个包。大功告成。
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") @WebAppConfiguration public 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。如此,再加入其它两个包。大功告成。
相关文章推荐
- SpringMVC Controller单元测试静态引入通配符问题
- SpringMVC Controller单元测试静态引入通配符问题
- SpringMVC Controller单元测试静态引入通配符问题
- SpringMVC Controller单元测试静态引入通配符问题
- SpringMVC Controller单元测试静态引入通配符问题
- SpringMVC Controller单元测试静态引入通配符问题
- SpringMVC Controller单元测试静态引入通配符问题
- SpringMVC Controller单元测试静态引入通配符问题
- SpringMVC Controller单元测试静态引入通配符问题
- SpringMVC Controller单元测试静态引入通配符问题
- SpringMVC Controller单元测试静态引入通配符问题
- spring MVC js css图片等静态资源无法加载问题
- Spring-MVC 登录拦截与静态资源冲突的问题
- 单元测试:(三) powermock + spring mvc mock 对 controller 层自动化测试入门
- J2EE for SpringMVC 映射静态资源独Js不成功的问题解决
- spring MVC Controller 实现接口后,不能解析@Controller注解问题
- springmvc controller 映射对象乱码问题
- springmvc+mybatis+maven项目集成的时候遇到一个问题,项目搭起来以后,http请求怎么都进不到controller中,页面直接返回404错误,控制台没有报任何错误,请大神帮我看看
- spring webmvc如何编写service 和controller的单元测试
- spring MVC controller 的单元测试 第一部分:配置