Spring Web MVC框架(十一) Spring Web MVC测试框架

来源:互联网 发布:伊芙蕾雅淘宝店 编辑:程序博客网 时间:2024/05/29 12:33

Spring 也提供了完善的测试框架,我们可以方便的测试Spring Web MVC应用程序。为了使用这个测试框架,我们需要添加它的依赖项。

compile group: 'org.springframework', name: 'spring-test', version: '4.3.6.RELEASE'

服务端测试

我们可以利用Spring提供的Mock对象来测试我们Spring程序的服务端行为。通过这些Mock对象,我们可以建立一个假的服务器,然后发送一些假的请求,来测试我们的程序。为了能简洁的编写测试代码,我们最好在代码中使用静态导入将MockMvcRequestBuilders.*MockMvcResultMatchers.*MockMvcBuilders.*引入到代码中。

建立测试环境

建立Spring Web MVC的测试环境和普通的Spring 单元测试略有不同。我们需要使用@WebAppConfiguration注解测试类。Spring知道这是一个Web MVC测试之后,就会使用@ContextConfiguration注解中的配置文件来创建一个WebApplicationContext,然后我们可以将其注入到测试类中。然后要做的事情就是创建MockMvc对象,我们大部分测试都要通过该对象进行。

@RunWith(SpringRunner.class)@WebAppConfiguration@ContextConfiguration("file:src/main/webapp/WEB-INF/applicationContext.xml")public class UserControllerTest {    @Autowired    private WebApplicationContext context;    private MockMvc mvc;    @Before    public void init() {        mvc = MockMvcBuilders.webAppContextSetup(context).build();    }}

当然,如果只需要测试某个控制器,我们完全可以不加载完整的配置文件。这时候可以使用MockMvcBuilders.standaloneSetup来仅使用Spring默认配置配置某个控制器。

public class SimpleTests {    private MockMvc mockMvc;    @Before    public void setup() {        this.mockMvc = MockMvcBuilders.standaloneSetup(new AccountController()).build();    }}

发起请求

这里假定代码中已经静态导入上面提到的一些类。

我们使用MockMvc的perform方法发起一个HTTP请求,这个请求可以是get、post等,然后我们还可以为请求设置accept等信息。

mockMvc.perform(post("/users/{id}", 42).accept(MediaType.ALL));

当然也可以发起文件上传请求。

mockMvc.perform(fileUpload("/upload").file("file", file.getBytes("UTF-8")));

我们可以直接在请求中包含参数。

mockMvc.perform(get("/users?user={foo}", "bar"));

也可以使用param方法传递参数,这种方式可以传递POST表单数据。

mockMvc.perform(post("/users").param("foo", "bar"));

如有需要,我们还可以为请求添加contextPath和servletPath。

mockMvc.perform(get("/myproject/contextpath/users").contextPath("/myproject").servletPath("/contextpath"))

期望结果

发起请求之后,我们需要验证请求是否正确处理。这时候需要在perform方法之后再调用andExpect方法。我们可以期望获得各种结果,最常用的就是获得各种响应码。下面的例子期望首页可以正常访问。当然status()方法也提供了其他了响应码方法来满足我们的需求。

mockMvc.perform(get("/index")).andExpect(status().isOk());

还可以期望结果的媒体类型。

mvc.perform(get("/users.xml"))        .andExpect(status().isOk())        .andExpect(content().contentType(MediaType.APPLICATION_XML));

有时候需要验证请求返回的模型,比如下面就断言结果会有错误。

mockMvc.perform(post("/updateInfo"))    .andExpect(status().isOk())    .andExpect(model().attributeHasErrors("user"));

某些情况下需要查看请求或响应的内容。我们可以调用Spring提供的print或log方法来打印信息或者记录日志。默认情况下print方法会将结果输出到System.out,而log方法会将日志记录到调试级别的org.springframework.test.web.servlet.result包下。

mockMvc.perform(post("/updateInfo"))    .andExpect(status().isOk())    .andDo(print())    .andExpect(model().attributeHasErrors("user"));

有时候需要详细检验返回结果。我们可以在所有期望方法的最后添加andReturn方法。该方法会返回一个MvcResult对象,我们可以调用该对象的各种get方法获取我们需要的信息。

MvcResult mvcResult = mockMvc.perform(post("/listUsers")).andExpect(status().isOk()).andReturn();

如果某些期望是所有方法都需要的,我们可以将它设置为共用的。但是一旦设置就无法更改。所以如果我们不需要某个共用期望的话就只能创建一个新的MockMvc对象了。

standaloneSetup(new UserController())    .alwaysExpect(status().isOk())    .alwaysExpect(content().contentType("application/json;charset=UTF-8"))    .build()

如果我们希望在单个控制器中添加过滤器的话,可以在建立MockMvc对象的时候指定过滤器。

mockMvc = standaloneSetup(new UserController()).addFilters(new CharacterEncodingFilter()).build();

spring-mvc-showcase是一个Spring官方开发的示例程序,包含了Spring Web MVC的例子和基本功能,也包含了所有的服务端测试代码。这也是一个很好的学习资源。

HtmlUnit集成

MockMvc虽然好用,但是毕竟是一个假的测试,它没有实际运行的服务器, 也不会进行实际的视图渲染、转发和重定向等操作。如果我们希望测试实际的HTML视图、JavaScript验证等功能,就需要使用HtmlUnit。

我们需要在项目中引用HtmlUnit的依赖。

compile group: 'net.sourceforge.htmlunit', name: 'htmlunit', version: '2.24'

然后初始化一个WebClient。

@AutowiredWebApplicationContext context;WebClient webClient;@Beforepublic void setup() {    webClient = MockMvcWebClientBuilder        .webAppContextSetup(context)        .build();}

这样配置的话,默认所有localhost下的请求就会自动通过MockMvc对象来访问,不需要实际HTTP连接,这方便我们本机测试。而其他域名会正常使用网络来连接,这可以让我们测试CDN等的状况。

然后我们可以使用WebClient来创建测试了。这里我直接贴Spring文档里的例子了。我们从例子中可以看到,WebClient的使用方法和使用普通的JavaScript操作DOM差不多。下面是创建请求的代码。

HtmlForm form = createMsgFormPage.getHtmlElementById("messageForm");HtmlTextInput summaryInput = createMsgFormPage.getHtmlElementById("summary");summaryInput.setValueAttribute("Spring Rocks");HtmlTextArea textInput = createMsgFormPage.getHtmlElementById("text");textInput.setText("In case you didn't know, Spring Rocks!");HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");HtmlPage newMessagePage = submit.click();

下面是执行验证的代码。这里的断言使用了AssertJ库。

assertThat(newMessagePage.getUrl().toString()).endsWith("/messages/123");String id = newMessagePage.getHtmlElementById("id").getTextContent();assertThat(id).isEqualTo("123");String summary = newMessagePage.getHtmlElementById("summary").getTextContent();assertThat(summary).isEqualTo("Spring Rocks");String text = newMessagePage.getHtmlElementById("text").getTextContent();assertThat(text).isEqualTo("In case you didn't know, Spring Rocks!");

从这里我们就可以看到直接使用HtmlUnit的缺点了,那就是代码笨重,不好看。Spring还提供了另外两个类库WebDriver和Geb来简化HtmlUnit的测试过程,详见Spring 参考文档 HtmlUnit集成

客户端的REST测试

如果需要客户端测试REST程序,Spring也提供了相关功能。直接来看Spring官方的例子。我们需要先创建一个RestTemplate对象,然后创建MockRestServiceServer并绑定到RestTemplate上。然后使用MockRestServiceServer的expect方法发起请求并测试结果。最后调用verify方法验证是否满足所有期望。这种方式不需要启动实际服务器,效率很高。

RestTemplate restTemplate = new RestTemplate();MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build();mockServer.expect(requestTo("/greeting")).andRespond(withSuccess());// 使用RestTemplate进行其他测试 ...mockServer.verify();

客户端测试也可以和服务端测试结合起来。我们可以利用MockMvc对象来创建RestTemplate,这样就会使用服务端的逻辑来测试代码而不需要启动实际服务器。

MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();this.restTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(mockMvc));// 使用RestTemplate进行其他测试 ...mockServer.verify();

参考资料

Spring 参考文档 15.6. Spring MVC Test Framework

0 0