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。如此,再加入其它两个包。大功告成。
- SpringMVC Controller单元测试静态引入通配符问题
- springmvc controller 单元测试
- 在springMVC中的controller写单元测试
- 单元测试SpringMVC带文件上传的controller
- springMvc找不到controller问题之一
- springmvc引入静态文件报404错误
- springmvc 静态资源引入报错
- springMVC 引入静态资源Js的方式
- 引入Controller
- 如何为 SpringMVC 编写单元测试:普通 Controller 测试
- SpringMVC中web层controller的单元测试小demo
- django 引入静态文件问题
- controller单元测试
- springMvc 静态文件问题
- springmvc控制器controller单例问题
- SpringMVC Controller命名冲突的问题
- SpringMVC Controller命名冲突的问题
- springmvc控制器controller单例问题
- mavn 报错Failure to transfer org.apache.maven.shared:maven-shared-components:pom:15 from http://repo.m
- CentOS And Ubuntu linux64位服务器 安装JDK gzip:stdin not in gzip format
- 链表及链表面试题
- 关于TCP的问题总结
- ArrayList去除集合中字符串的重复值
- SpringMVC Controller单元测试静态引入通配符问题
- java中四种访问权限
- html<textarea>根据输入自适应高度
- mybatis 1:1,1:n,n:n关系
- LSM树由来、设计思想以及应用到HBase的索引
- xxx cannot be resolved to a type 错误解决方法
- 如何在 React Native 中写一个自定义模块
- show processlist
- iOS设置键盘上Return按键及点击操作