Spring-mvc junit单元测试中 如何回滚?
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration(value = "src/main/webapp")
@ContextHierarchy({
@ContextConfiguration(name = "parent", locations = "classpath:./spring/applicationContext.xml")
})
public class MessageTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void testTempPage() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/msg/temp/page/v1"));
}
@Test
public void testAddTemp() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/msg/temp/add/v1").param("name", "新测试3")
.param("content", "nice roll"));
}
}
testAddTemp添加了一天记录,如何使这个测试添加的动作自动回滚?
1 个回答
1
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration(value = "src/main/webapp")
@ContextHierarchy({
@ContextConfiguration(name = "parent", locations = "classpath:./spring/applicationContext.xml")
})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class MessageTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void testTempPage() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/msg/temp/page/v1"));
}
@Test
@Rollback(true)
public void testAddTemp() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/msg/temp/add/v1").param("name", "新测试3")
.param("content", "nice roll"));
}
}
一般来说在你的测试类上加上@Transactional就足够了,Spring默认是会回滚的。
更简便的做法:直接继承AbstractTransactionalJUnit4SpringContextTests
Spring-mvc junit单元测试中 如何回滚?的更多相关文章
- Spring mvc注解方式使用事务回滚
项目名:1ma1ma jdbc.xml <bean id="dataSource" class="org.apache.commons.dbcp.BasicDat ...
- spring MVC junit单元测试 各test之间共享变量
使用静态变量 private static String iPSetCode=null;
- (转)spring异常抛出触发事务回滚策略
背景:在面试时候问到事务方法在调用过程中出现异常,是否会传递的问题,平时接触的比较少,有些懵逼. spring异常抛出触发事务回滚策略 Spring.EJB的声明式事务默认情况下都是在抛出unchec ...
- Spring异常抛出触发事务回滚
Spring.EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚 /** * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*) ...
- Spring AOP声明式事务异常回滚(转)
转:http://hi.baidu.com/iduany/item/20f8f8ed24e1dec5bbf37df7 Spring AOP声明式事务异常回滚 近日测试用例,发现这样一个现象:在业务代码 ...
- Spring AOP声明式事务异常回滚
近日测试用例,发现这样一个现象:在业务代码中,有如下两种情况,比如:throw new RuntimeException("xxxxxxxxxxxx"); 事物回滚throw ne ...
- spring mvc在Controller中获取ApplicationContext
spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...
- Spring MVC 使用tomcat中配置的数据源
Spring MVC 使用tomcat中配置的数据源 配置tomcat数据源 打开tomcat目录下的conf目录,编辑sever.xml目录.在<GlobalNamingResources&g ...
- Spring MVC 了解WebApplicationContext中特殊的bean类型
Spring MVC 了解WebApplicationContext中特殊的bean类型 Spring的DispatcherServlet使用了特殊的bean来处理请求.渲染视图等,这些特定的bean ...
随机推荐
- 【前端JS】input textarea 默认文字,点击消失
如题.前端页面的 input textarea 有时候须要显示默认文字以提示用户,下面为实现代码,以 input 为例.textarea 能够直接搬用 HTML <input type=&quo ...
- Js 正则表达式知识测试
本文对javascript中正则表达式进行了总结汇总,将知识点和注意点都理了一下,并附上2个练习题,供大家参考学习. 正则表达式: 1.什么是RegExp?RegExp是正则表达式的缩写.RegExp ...
- Java基础知识强化73:正则表达式之分割功能
1. 分割功能: 使用String的split方法,split方法:根据给定正则表达式的匹配拆分字符串.如下: public String[] split(String regex): 2. 案例: ...
- C# Byte[]数组读取和写入文件
这个项目我用的是asp.net构建的,代码如下 protected void ByteToString_Click(object sender, EventArgs e) { string conte ...
- OD: SafeSEH
SafeSEH 对异常处理的保护原理 在 Windows XP sp2 以及之后的版本中,微软引入了 S.E.H 校验机制 SafeSEH.SafeSEH 需要 OS 和 Compiler 的双重支持 ...
- spring的xml的property和constructor-arg的解析
参考文档: http://zzy7182.iteye.com/blog/1153473
- chrome Provisional headers are shown错误提示
1.一般出现这个错误是请求没有发送成功 可能原因:在上传文件或ajax上传时指定的timeout,过时时间小 其他资料: http://www.duanzhihe.com/575.html http: ...
- Java sql helper[转]
原文:http://www.cnblogs.com/beijiguangyong/archive/2011/12/10/2302737.html package sql; import java.sq ...
- 解析xml的几种方式
http://blog.csdn.net/smcwwh/article/details/7183869
- cocos2dx之触摸事件
要使精灵能够接收到触摸事件,无非要做三件事. 注册触摸事件; 接收触摸事件; 处理触摸事件. 下面就从这三点出发,来了解一下精灵如何响应触摸事件. 1.注册触摸事件 精灵类Poker继承Sprite和 ...