解决使用JUNIT测试DAO事务一直ROLL BACK问题

 

 1 package com.ithheima.repository;
 2
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.test.annotation.Rollback;
 7 import org.springframework.test.context.ContextConfiguration;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9 import org.springframework.transaction.annotation.Propagation;
10 import org.springframework.transaction.annotation.Transactional;
11
12 import com.itheima.entity.Customer;
13 import com.itheima.repository.CustomerRepository;
14
15 @RunWith(SpringJUnit4ClassRunner.class)
16 @ContextConfiguration("classpath:applicationContext.xml")
17 @Transactional(propagation=Propagation.REQUIRED)
18 public class CustomerRepositoryTest {
19
20     @Autowired
21     private CustomerRepository cr;
22
23     @Test
24     public void testSave() {
25         Customer c = new Customer();
26         c.setName("测试");
27         cr.save(c);
28         System.out.println(c);
29     }
30
31     @Test
32     public void testGet() {
33         Customer c = cr.findOne(7);
34         System.out.println(c);
35     }
36
37 }

每一次执行完后都会有事务回滚。

解决办法:在测试用例类上或者测试方法上@Rollback(false)

11:24:11,252 DEBUG ResourceRegistryStandardImpl:104 - HHH000387: ResultSet‘s statement was not registered
Customer [id=12, name=测试, orders=null]
11:24:11,254 DEBUG TransactionImpl:86 - rolling back
11:24:11,263 DEBUG SessionFactoryImpl:1052 - HHH000031: Closing
11:24:11,264 DEBUG AbstractServiceRegistryImpl:389 - Implicitly destroying ServiceRegistry on de-registration of all child ServiceRegistries
11:24:11,264 DEBUG BootstrapServiceRegistryImpl:286 - Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries
11:24:11,265 DEBUG EntityManagerFactoryRegistry:91 - Remove: name=default

时间: 2024-08-29 02:06:02

解决使用JUNIT测试DAO事务一直ROLL BACK问题的相关文章

spring hibernate 事务整合 使用测试类 事务不自动提交的问题!!!

使用JUnit 测试hibernate 事务管理的时候应注意 ,测试类完成是默认回滚的. 所以只能查询数据库却不能增删改数据库. 应该在测试类上面加上注解 @Rollback(false)  表似默认不回滚. package TestHibernate; import java.util.List; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import or

Junit测试Controller(MockMVC使用),传输@RequestBody数据解决办法

一.单元测试的目的 简单来说就是在我们增加或者改动一些代码以后对所有逻辑的一个检测,尤其是在我们后期修改后(不论是增加新功能,修改bug),都可以做到重新测试的工作.以减少我们在发布的时候出现更过甚至是出现之前解决了的问题再次重现. 这里主要是使用MockMvc对我们的系统的Controller进行单元测试. 对数据库的操作使用事务实现回滚,及对数据库的增删改方法结束后将会还远数据库. 二.MockMvc的使用 1.首先我们上一个例子, import org.apache.commons.log

无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)

1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.demo1" /> 上面targetPackage指定的包要和应用的package相同. (2)在清单文件中ap

【Java EE 学习第19天】【使用过滤器实现全站压缩】【使用ThreadLocal模式解决跨DAO事务回滚问题】

一.使用过滤器实现全站压缩 1.目标:对网站的所有JSP页面进行页面压缩,减少用户流量的使用.但是对图片和视频不进行压缩,因为图片和视频的压缩率很小,而且处理所需要的服务器资源很大. 2.实现原理: (1)使用GZIPOutputStream工具对数据进行压缩,中间借助了ByteArrayOutputStream类进行结果的存储. (2)使用过滤器对浏览器请求进行拦截,通过自定义HttpServletResponse类(使用包装模式),重写getWriter方法,使得写出的目的地转变成ByteA

Spring项目JUnit测试报错ClassNotFoundException解决、

MyEclipse项目上有红色感叹号,各包显示正常.用JUnit测试部分能运行,部分报错,报错如下: Class not found n_jdbc.UserDaoTestjava.lang.ClassNotFoundException: n_jdbc.UserDaoTest at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native

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

最近在搭建Maven+springMVC+hibernate项目,正常启动项目时一切正常,但JUNIT测试时出现报ClassNotFoundException错误,经过仔细排查发现没有生成class文件. 现在解决如下:

基于Spring的可复用的Junit测试类的设计

平时我们做SSH项目的时候避免不了要做单元测试,而且很多时候,我们是不希望单元测试的结果对真正的数据库有影响的,那么我们就需要使用事务来管理了. JUnit测试基类如下: package com.sms.test.base; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.Te

Android Junit测试框架

对应用进行单元测试: 使用Junit测试框架,是正规Android开发的必用技术.在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性. 1.配置指令集和函数库: (1)配置指令集,指定要测试的应用程序 需要在AndroidManifest.xml的instrumentation中增加InstrumentationTestRunner,并指定要测试的包名. AndroidManifest.xml中会添加代码: <instrumentation android:targetPacka

002杰信-陌生的maven-web项目整改成我们熟悉的Web架构;classpath的含义;ssm框架的整合;junit测试

这篇博客的资源来源于创智播客,先在此申明.这篇博客的出发点是jk项目,传智的做法是Maven的web模板生成的,但是这样子的结构目录与我们熟知的Web项目的结构目录相差很大,所以要按照我们熟知的项目结构来.这篇文章涉及到的最重要的是:1.加载各种配置文件时经常涉及到classpath,这个东西,要搞搞清楚,在web项目代表的是什么( WEB-INF文件夹下的classes目录).2.还有就是做一个项目时,建包的目录,3.用junit去测试框架的整合. 传智的做法是Maven的web模板生成的: