解决使用JUNIT测试DAO事务一直ROLL BACK问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决使用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
以上是关于解决使用JUNIT测试DAO事务一直ROLL BACK问题的主要内容,如果未能解决你的问题,请参考以下文章
SpringBootTest 用于 DAO JUnit 测试
使用 JUnit 和 Mockito 对 DAO 类进行单元测试