码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-07-30 12:52:03      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:sys   ica   factor   red   conf   cts   cat   resource   问题   

 

 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

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

标签:sys   ica   factor   red   conf   cts   cat   resource   问题   

原文地址:http://www.cnblogs.com/ilovezihan/p/7258636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!