现在越来越多人使用SpringMvc来开发系统,在开发中可定需要对后台url地址请求测试,并且返回预期的结果!

Spring提供的测试类MockMvc来进行url地址请求测试,使用方方式:

package com.cml.controller;





import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;





import org.junit.Before;

import org.junit.Test;

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.ResultActions;

import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;

import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import org.springframework.web.context.WebApplicationContext;





@RunWith(SpringJUnit4ClassRunner.class)

@WebAppConfiguration

@ContextConfiguration(locations = { "classpath:applicationContext.xml",

"classpath:mvc.xml" })

public class ExampleTests

{





@Autowired

private WebApplicationContext wac;





private MockMvc mockMvc;





@Before

public void setup()

{

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

}





@Test

public void getAccount() throws Exception

{

System.out.println("返回json。。。。。。。。。。。");





ResultActions actions = this.mockMvc.perform(

get("/ll.mvc").param("name", "DDDDDDD").accept(

MediaType.APPLICATION_JSON)).andExpect(status().isOk());





System.out.println("测试成功");



}

}

解释:

@WebAppConfiguration 声明为web环境测试

@ContextConfiguration(locations = { "classpath:applicationContext.xml",

"classpath:mvc.xml" }) spring 和mvc的配置文件位置

@RunWith(SpringJUnit4ClassRunner.class)使用spring测试

最新文章

  1. SDK接入(2)之Android Google Play内支付(in-app Billing)接入
  2. 终端检测HTTPS服务端
  3. 为应用程序池 'DefaultAppPool' 提供服务的进程无法响应Ping
  4. tp框架查询
  5. codeforces A. Group of Students 解题报告
  6. robotframework笔记7
  7. 创建一个struts2的HelloWorld
  8. UVA 10497 - Sweet Child Makes Trouble 高精度DP
  9. SOURCES的文件格式
  10. (原创)CityEngine 2014和ArcGIS 10.3冲突问题的解决
  11. windows7+iis7+php的配置
  12. Visual Studio 2015使用EF6的ModelFirst模式添加实体数据模型缺少tt文件问题
  13. 高性能mysql主存架构
  14. [bzoj2288][POJ Challenge]生日礼物
  15. 【JavaScript】jQuery
  16. L2-019. 悄悄关注
  17. syslog之二:syslog协议及rsyslog服务全解析
  18. Linux入侵问题排查
  19. 转:利用Eclipse CDT 阅读C/C++代码
  20. 彻底去除Win10“快速访问”

热门文章

  1. 硬盘性能测试工具之bonnie++
  2. ubuntu(Linux)下,查看CPU性能集合
  3. ES6系列-什么是ES6?新手应该怎么理解
  4. Python开发基础之Python常用的数据类型
  5. 面试官:你对Redis缓存了解吗?面对这11道面试题你是否有很多问号?
  6. PHP把PNG图片转化为JPG时透明背景变黑色
  7. 从一个数对列中找出素数对用于RAS加密的C++程序
  8. 有关for循环的一些东西
  9. CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)
  10. 图论--割点--Tarjan模板