如今越来越多人使用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測试

spring test---測试SpringMvc初识的更多相关文章

  1. spring test---测试SpringMvc初识

    现在越来越多人使用SpringMvc来开发系统,在开发中可定需要对后台url地址请求测试,并且返回预期的结果! Spring提供的测试类MockMvc来进行url地址请求测试,使用方方式: packa ...

  2. 搭建项目Maven+springMVC+hibernate时,JUnit測试出现报ClassNotFoundException错误的解决

    近期在搭建Maven+springMVC+hibernate项目,正常启动项目时一切正常.但JUNIT測试时出现报ClassNotFoundException错误,经过细致排查发现没有生成class文 ...

  3. redis在linux下安装并測试(在spring下调用)

    官网帮助文档例如以下 Installation Download, extract and compile Redis with: $ wget http://download.redis.io/re ...

  4. 文件下载-SpringMVC中測试

    直接改动文件路径就能够.其它都不须要改动,帮助类已经为大家写好,可直接使用 1.Scroller: /** * 下载文件 * @author liupeng * @param request * @p ...

  5. springMVC4(5)RestTemplate控制层单元測试

    在前面我们进行web測试,总要在游览器进行.数据组装.请求方法更给等都极为麻烦. RestTemplate是Spring提供的一个web层測试模板类,我们能够通过RestTemplate在client ...

  6. JMeter使用记录2 -- Web測试

    Web測试用例 新建线程组.配置起3线程,循环1次.1秒启动全部线程.进行login.循环1个主要页面的读写,并对另外一个主要页面进行读操作,最后logout并在aggregate graph/gra ...

  7. eclipse下的ssh框架整合过程及測试

    最近在搭建Stuts2+hibernate+spring的框架,网上看的教程,大部分都是非常easy的步骤.没有比較具体的步骤以及每一个步骤完毕之后怎样检查是否配置成功.下面是笔者依据自己搭建的过程进 ...

  8. Maven实现Web应用集成測试自己主动化 -- 測试自己主动化(WebTest Maven Plugin)

    近期在appfuse看到使用webtest-maven-plugin实现Web应用的集成測试,研究了下.感觉很不错.对于Web应用自己主动构建很有帮助,在性能測试之前能够保证Web应用的基本功能工作正 ...

  9. 基于redis集群实现的分布式锁,可用于秒杀商品的库存数量管理,有測试代码(何志雄)

    转载请标明出处. 在分布式系统中,常常会出现须要竞争同一资源的情况,本代码基于redis3.0.1+jedis2.7.1实现了分布式锁. redis集群的搭建,请见我的另外一篇文章:<>& ...

随机推荐

  1. angular 创建项目

    介绍有很多可用的工具可以帮助你开发AngularJS 应用,那些非常复杂的框架不在我的讨论范围之中,这也是我开始这系列教程的原因.在第一部分,我们掌握了angularjs框架的基本结构,开发了第一应用 ...

  2. appium+python自动化-adb shell模拟点击事件(input tap)

    前言 appium有时候定位一个元素很难定位到,或者说明明定位到这个元素了,却无法点击,这个时候该怎么办呢? 求助大神是没用的,点击不了就是点击不了,appium不是万能的,这个时候应该转换思路,换其 ...

  3. Builder(构造者)

    Builder(构造者) <?php class Product { private $name; public function setName($name) { $this->name ...

  4. BNUOJ 1207 滑雪

    滑雪 Time Limit: 1000ms Memory Limit: 65536KB   This problem will be judged on PKU. Original ID: 10886 ...

  5. Django之model admin自定义后台管理

    Admin管理界面是django的杀手级应用.它读取你模式中的元数据,然后提供给你一个强大而且可以使用的界面,网站管理者可以用它立即向网站中添加内容. 比如,数据表如下: from django.db ...

  6. bzoj1063【Noi2008】道路设计

    题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1063 用一种划分方式将树划为重链和轻链,使得所有点到根节点的路径经过的轻链最大值最小 sol: ...

  7. 刷题总结——竞赛得分(ssoj)

    题目: 题目描述 ZZH 在经历了无数次学科竞赛的失败以后,得到了一个真理:做一题就要对一题!但是要完全正确地做对一题是要花很多时间(包括调试时间),而竞赛的时间有限.所以开始做题之前最好先认真审题, ...

  8. 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame

    http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...

  9. out.print和out.write

    这是一个JSP页面: <%@ page language="java" import="java.util.*"  %> <%@ page p ...

  10. scrapy介绍及源码分析

    一 简介 Scrapy基于事件驱动网络框架 Twisted 编写.因此,Scrapy基于并发性考虑由非阻塞(即异步)的实现. 官方文档 :https://docs.scrapy.org/en/late ...