spring test---測试SpringMvc初识
如今越来越多人使用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初识的更多相关文章
- spring test---测试SpringMvc初识
现在越来越多人使用SpringMvc来开发系统,在开发中可定需要对后台url地址请求测试,并且返回预期的结果! Spring提供的测试类MockMvc来进行url地址请求测试,使用方方式: packa ...
- 搭建项目Maven+springMVC+hibernate时,JUnit測试出现报ClassNotFoundException错误的解决
近期在搭建Maven+springMVC+hibernate项目,正常启动项目时一切正常.但JUNIT測试时出现报ClassNotFoundException错误,经过细致排查发现没有生成class文 ...
- redis在linux下安装并測试(在spring下调用)
官网帮助文档例如以下 Installation Download, extract and compile Redis with: $ wget http://download.redis.io/re ...
- 文件下载-SpringMVC中測试
直接改动文件路径就能够.其它都不须要改动,帮助类已经为大家写好,可直接使用 1.Scroller: /** * 下载文件 * @author liupeng * @param request * @p ...
- springMVC4(5)RestTemplate控制层单元測试
在前面我们进行web測试,总要在游览器进行.数据组装.请求方法更给等都极为麻烦. RestTemplate是Spring提供的一个web层測试模板类,我们能够通过RestTemplate在client ...
- JMeter使用记录2 -- Web測试
Web測试用例 新建线程组.配置起3线程,循环1次.1秒启动全部线程.进行login.循环1个主要页面的读写,并对另外一个主要页面进行读操作,最后logout并在aggregate graph/gra ...
- eclipse下的ssh框架整合过程及測试
最近在搭建Stuts2+hibernate+spring的框架,网上看的教程,大部分都是非常easy的步骤.没有比較具体的步骤以及每一个步骤完毕之后怎样检查是否配置成功.下面是笔者依据自己搭建的过程进 ...
- Maven实现Web应用集成測试自己主动化 -- 測试自己主动化(WebTest Maven Plugin)
近期在appfuse看到使用webtest-maven-plugin实现Web应用的集成測试,研究了下.感觉很不错.对于Web应用自己主动构建很有帮助,在性能測试之前能够保证Web应用的基本功能工作正 ...
- 基于redis集群实现的分布式锁,可用于秒杀商品的库存数量管理,有測试代码(何志雄)
转载请标明出处. 在分布式系统中,常常会出现须要竞争同一资源的情况,本代码基于redis3.0.1+jedis2.7.1实现了分布式锁. redis集群的搭建,请见我的另外一篇文章:<>& ...
随机推荐
- layer2-1 二层
一 概述 一层的相关介绍 CSMA/CD 网桥和交换机的区别 冲突 共享 端口密度 性能 功能 交换机的三种主流转发方式 存储转发 完整的收到 ...
- 树状数组 Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains
C. Fountains time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Java类方法 类变量
类变量就是静态变量,类方法就是静态方法. 在理解类变量.类方法之前先看一段代码: class Person{ int age ; String name; static int totalFee; p ...
- Python 打印嵌套list中每个数据(遍历列表)
new_list = ["H1","H2",1999] for each_list in new_list: print (each_list); 若列表中包含 ...
- 在 Linux 实例上自动安装并运行 VNC Server
原文网址:https://help.aliyun.com/knowledge_detail/41181.html?spm=5176.8208715.110.11.4c184ae8mlC7Yy 您可以使 ...
- ElasticSearch分词器
什么是分词器? 分词器,是将用户输入的一段文本,分析成符合逻辑的一种工具.到目前为止呢,分词器没有办法做到完全的符合人们的要求.和我们有关的分词器有英文的和中文的.英文的分词器过程:输入文本-关键词切 ...
- 济南学习 Day 5 T3 am
[题目描述] 众所不知,rly现在不会玩国际象棋.但是,作为一个OIer,rly当然做过八皇后问题.在这里再啰嗦几句,皇后可以攻击到同行同列同对角线,在 n*n的棋盘中,摆放n个皇后使它们互相不能攻击 ...
- indexedDB 增删改查
/** * 打开数据库 */ export function openDB() { return new Promise((resolve, reject) => { let indexedDB ...
- linux 抓取访问量排行
需求: 分析图片服务日志,把日志(每个图片访问次数*图片大小的总和)排行,取top10,也就是计算每个url的总访问大小 语句: awk '{a[$1]+=$10;}END{for(i in a){p ...
- 三类(创结行),23种设计模式,速记理解法!PHP
一,创建型设计模式 1.FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说“来四个鸡翅”就行了.麦当劳 ...