package com.ieou.ms_backend.controller;

 import com.google.gson.Gson;
import com.ieou.ms_backend.dto.account.CreateAccountReq;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
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 static org.junit.Assert.*; /**
* created by wyz on 2019/5/6
*/ @SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class AccountControllerTest { @Autowired
private WebApplicationContext wac; private MockMvc mockMvc;
private String url = "/ms_backend/account/"; @Before
public void setUp() throws Exception{
//初始化MockMvc对象
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
} //GET 请求 @Test
public void accountList() throws Exception { MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(url + "accountList")
.param("companyName", "wang")
.header("access_token", "accessToken"); mockHttpServletRequestBuilder.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON_UTF8); ResultActions resultActions = mockMvc.perform(mockHttpServletRequestBuilder);
resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8");
resultActions.andExpect(MockMvcResultMatchers.status().isOk());
resultActions.andDo(MockMvcResultHandlers.print());
} @Test
public void removeAccount() {
} //post 请求 @RequestBody @Test
@Transactional
@Rollback() // 事务自动回滚,默认是true。可以不写
public void createAccount() throws Exception { CreateAccountReq req = new CreateAccountReq(); MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.post(url + "createAccount")
.header("access_token", "accessToken"); mockHttpServletRequestBuilder.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(new Gson().toJson(req)); // post请求 ResultActions resultActions = mockMvc.perform(mockHttpServletRequestBuilder);
resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8");
resultActions.andExpect(MockMvcResultMatchers.status().isOk());
resultActions.andDo(MockMvcResultHandlers.print());
}
}

MockMvc 进行 controller层单元测试 事务自动回滚 完整实例的更多相关文章

  1. flask-sqlalchemy、pytest 的单元测试和事务自动回滚

    flask-sqlalchemy.pytest 的单元测试和事务自动回滚 使用 flask-sqlalchemy 做数据库时,单元测试可以帮助发现一些可能意想不到的问题,像 delete-cascad ...

  2. Spring事务为什么不会自动回滚?Spring事务怎样才会自动回滚?事务自动回滚条件及手动回滚

    原文:https://blog.csdn.net/qq_32331073/article/details/76508147 更多Spring事务问题请访问链接:Spring事务回滚问题疑难详解 在此, ...

  3. mysql 事务中如果有sql语句出错,会导致自动回滚吗?

    事务,我们都知道具有原子性,操作要么全部成功,要么全部失败.但是有可能会造成误解. 我们先准备一张表,来进行测试 CREATE TABLE `name` ( `id` int(11) unsigned ...

  4. spring事务什么时候会自动回滚

    在java中异常的基类为Throwable,他有两个子类xception与Errors.同时RuntimeException就是Exception的子类,只有RuntimeException才会进行回 ...

  5. 事务框架之声明事务(自动开启,自动提交,自动回滚)Spring AOP 封装

    利用Spring AOP 封装事务类,自己的在方法前begin 事务,完成后提交事务,有异常回滚事务 比起之前的编程式事务,AOP将事务的开启与提交写在了环绕通知里面,回滚写在异常通知里面,找到指定的 ...

  6. Spring事务不回滚原因分析

    Synchronized用于线程间的数据共享,而ThreadLocal则用于线程间的数据隔离. 在我完成一个项目的时候,遇到了一个Spring事务不回滚的问题,通过aspectJ和@Transacti ...

  7. Spring,SpringMvc配置常见的坑,注解的使用注意事项,applicationContext.xml和spring.mvc.xml配置注意事项,spring中的事务失效,事务不回滚原因

    1.Spring中的applicationContext.xml配置错误导致的异常 异常信息: org.apache.ibatis.binding.BindingException: Invalid ...

  8. Spring声明式事务不回滚问题

    疑问,确实像往常一样在service上添加了注解 @Transactional,为什么查询数据库时还是发现有数据不一致的情况,想想肯定是事务没起作用,出现异常的时候数据没有回滚.于是就对相关代码进行了 ...

  9. springmvc mybatis 声明式事务管理回滚失效,(checked回滚)捕捉异常,传输错误信息

    一.知识点及问题 后端框架: Spring .Spring mvc .mybatis 业务需求: client先从服务端获取用户大量信息到client,编辑完毕之后统一Post至服务端,对于数据的改动 ...

随机推荐

  1. Diworth定理

    Diworth定理 一个序列中下降子序列的最少划分数个数等于最长上升子序列的长度. 一个序列中上升子序列的最少划分数个数等于最长下降子序列的长度. 每句中的前后两者互为偏序关系. 例题: Descri ...

  2. 初识QuartusII 9.0(破解,半加器的仿真,综合:下)

    完成波形的随机设置(A,B任意给定高低电平即可,只是当作测试信号),选择任务栏Assignments[Setings],设置Simulation mode为functional,其余保持不变点击ok. ...

  3. 2017.10.7 国庆清北 D7T2 第k大区间

    题目描述 定义一个长度为奇数的区间的值为其所包含的的元素的中位数. 现给出n个数,求将所有长度为奇数的区间的值排序后,第K大的值为多少. 输入输出格式 输入格式: 输入文件名为kth.in. 第一行两 ...

  4. python中isinstance函数

    1.描述 python中isinstance()函数,是python中的一个内置函数,用来判断一个函数是否是一个已知的类型,类似type(). 2.语法 isinstance(object,class ...

  5. 7、CentOS6 编译安装

    LAMP组合的编译安装: httpd*php modules:把php编译成httpd的DSO对象 prefork:libphp5 event,worker : libphp5-zts cgi fpm ...

  6. Java 中Math常用方法

    import java.text.SimpleDateFormat; import java.util.Date; public class Test4 { public static void ma ...

  7. Ubuntu安装邮件服务器

    Ubuntu搭建邮件服务器 此文我们使用Postfix来搭建邮箱服务器,Postifx是一个SMTP服务器.SMTP服务器也被称为MTA(message transfer agent) 一.安装pos ...

  8. 2019-2020-1 20175313 《信息安全系统设计基础》实现mypwd

    目录 MyPWD 一.题目要求 二.题目理解 三.需求分析 四.设计思路 五.伪代码分析 六.码云链接 七.运行结果截图 MyPWD 一.题目要求 学习pwd命令 研究pwd实现需要的系统调用(man ...

  9. python3 ssl导入失败

    make LibreSSL 2.6.4 and earlier do not provide the necessary APIs /root/Python-3.7.0/build/lib.linux ...

  10. Binder 原理剖析***

    一. 前言 希望更加深入理解 Binder 实现机制的,可以阅读文末的参考资料以及相关源码. 二. Binder 概述 简单介绍下什么是 Binder.Binder 是一种进程间通信机制,基于开源的 ...