SpringMVC Junit Demo
package com.zchx.test; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.mock.web.MockHttpSession;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
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.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature; /**
* 测试类
*
* @version V1.0
* @author songxiaotong
* @date 2018年6月28日 下午2:10:46
* @Description
*/
@SuppressWarnings("deprecation")
@RunWith(SpringJUnit4ClassRunner.class)
// 设置读取的spring配置文件
@ContextConfiguration({ "classpath*:servlet-context.xml" })
// 当然 你可以声明一个事务管理 每个单元测试都进行事务回滚 无论成功与否
// @TransactionConfiguration(defaultRollback = true)
@Rollback(value = true)
@Transactional
@WebAppConfiguration
public class MyTest2 { /**
* 日志记录工具
*/
private static final Logger LOGGER = LogManager.getLogger(MyTest2.class); /**
* WebApplicationContext
*/
@Autowired
private WebApplicationContext wac; private MockMvc mockMvc; /**
* 初始化mvc请求对象
*
* @throws Exception
*/
@Before
public void setUp() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
} @Test
public void test() {
try {
MockHttpSession session = new MockHttpSession(); JSONObject param = new JSONObject();
param.put("test1", "1");
param.put("test2", "2");
param.put("test3", "3"); MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/zchx/test/test");
requestBuilder.contentType(MediaType.APPLICATION_JSON); requestBuilder.content(JSONObject.toJSONString(param, SerializerFeature.WriteMapNullValue));
// requestBuilder.content(param.toString()); requestBuilder.session(session); // 执行一个请求;
ResultActions result = mockMvc.perform(requestBuilder); // 添加执行完成后的断言
result.andExpect(MockMvcResultMatchers.status().isOk()); // 添加一个结果处理器,表示要对结果做点什么事情,比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息。
result.andDo(MockMvcResultHandlers.print()); // 表示执行完成后返回相应的结果。
MvcResult mvcResult = result.andReturn(); LOGGER.info(mvcResult.getResponse().getContentAsString());
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
} }
SpringMVC Junit Demo的更多相关文章
- 【SSH系列】深入浅出SpringMvc+入门Demo
Spring MVC框架是有一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC和Struts.Strut ...
- appium+java+junit demo运行
对java熟悉一些,所以想用java把appium给做起来.今天用myeclipse给环境和Demo弄了一下,网上没有一篇全套资料的. 各块环境的搭建: 1.安装appium客户端,省略下载和安装步骤 ...
- Spring+MyBatis+SpringMvc整合Demo
客户关系管理系统demo 项目分析 该demo使用技术及环境:ssm+maven+bootstrap+jsp+mysql+idea+jdk1.8 需求:客户管理,实现客户列表分页显示如下图 项目开始 ...
- SpringMVC小demo解析
第一次实际接触SpringMVC,之前在教程网站上看得是概念性的. SpringMVC是属于Java框架SSM中的一环 在做了一个小demo后发现原来编程如此简单. 首先建立动态网页项目(Dynami ...
- spring-mvc junit测试
import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; impor ...
- SpringMVC 入门demo
(1)新建Spring项目 (2)添加所需要的jar包 spring的5+2: spring-core.jar spring.beans.jar spring-context.jar spring-e ...
- 一个SpringMVC简单Demo中出现的错误
最近在学springmvc 一个简答的Springmvc配置包括如下步骤: 1.在 web.xml 文件中配置 DispatcherServlet (该中央控制器相当于 MVC 模式中的 C),还可以 ...
- springmvc入门demo
目录结构: package com.wyl; import org.springframework.stereotype.Controller; import org.springframework. ...
- springmvc junit测试
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.ann ...
随机推荐
- vue调用高德地图:vue-amap
前言:之前没有接触过页面调用地图的项目,某次面试,老板要求我用vue-amap调用高德地图,回家以后,我去网上查了一些案例和教程,看似很简单的引入调用,我却整整弄了一宿,还没弄出来!!!百般无奈之下, ...
- python多版本共存pip指向问题
这两天一致被一个问题困扰,电脑里装了anaconda和python3.7,在命令行里输入python,想要python3.7,出现的确实python3.6,或使用pip安装包时,不知道是装在里pyth ...
- jQuery中的CSS(四)
1. css(name|pro|[,val|fn]), 访问匹配元素的样式属性 jQuery 1.8中,当你使用CSS属性在css()或animate()中,我们将根据浏览器自动加上前缀(在适当的时候 ...
- CF-1208 C.Magic Grid
题目 大意:构造一个n行n列的矩阵,使得每一行,每一列的异或和都相等,n是4的倍数. 先看4*4的矩阵,我们很容易构造出符合要求的矩阵,比如 0 1 2 3 4 5 6 ...
- keras.preprocessing.text.Tokenizer
说明:num_words的参数设置,对应着sequences_to_matrix方法返回的arrray的shape[1],用于约束返回数组的第2个维度.对texts_to_sequences(text ...
- python实现语音信号处理常用度量方法
信噪比(SNR) 有用信号功率与噪声功率的比(此处功率为平均功率),也等于幅度比的平方 $$SNR(dB)=10\log_{10}\frac{\sum_{n=0}^{N-1}s^2(n)}{\sum_ ...
- Flutter命令突然无响应、vscode突然无法连接到IOS模拟器
这周过来突然flutter相关命令运行都没有响应, 打开任务管理器,关闭所有dart进程即可解决
- 海边拾贝-G-若干有用的文章(乱序,经常更新)
若干有用的文章,乱序版本.会经常性修改. 若干Python模块的介绍不错 https://www.cnblogs.com/sui776265233/category/1239819.html ...
- NORDIC超低功耗蓝牙4.0 NRF51822QFAA和NRF51802QFAA
51822-QFAA和51802-QFAA在FLASH RAM的容量没有差别, 两者都是出自NORDIC原厂,: 区别在于: 1.接收灵敏度 51802是-91dBm;51822是-93dBm,这个差 ...
- CSS3 滤镜Filter亮度动画
CSS3 滤镜Filter亮度动画 -webkit-filter:brightness 值越高 亮度越亮<pre><!DOCTYPE html><html lang=&q ...