午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java - 單元測(cè)試,怎么讓spring管理事務(wù)又不污染數(shù)據(jù)庫(kù)

瀏覽:146日期:2023-11-07 08:29:14

問(wèn)題描述

我正在嘗試用springjunit測(cè)試 DAO 的方法,我看到網(wǎng)上的一些做法是使用 spring 的聲明式事務(wù)管理(即@Transactional)進(jìn)行事務(wù)操作,說(shuō)是這樣在測(cè)試完成之后能夠spring會(huì)讓測(cè)試的方法回滾,從而達(dá)到測(cè)試的目的。然后我按照這一做法對(duì)dao中添加操作的方法進(jìn)行了測(cè)試,發(fā)現(xiàn)事務(wù)進(jìn)行提交后,回滾沒(méi)有成功,數(shù)據(jù)庫(kù)中多出來(lái)了我進(jìn)行測(cè)試的數(shù)據(jù)。一開(kāi)始我以為是spring沒(méi)有進(jìn)行回滾,但是后面觀察控制臺(tái)打印信息發(fā)現(xiàn)是有rollback信息的,但是為什么會(huì)失敗呢,就不清楚。我查到一些相關(guān)的方案,但是我發(fā)現(xiàn)并沒(méi)能解決我的問(wèn)題。很困擾,特來(lái)請(qǐng)教各位,望不吝指教。

以下是我的代碼和相應(yīng)配置

DAOImpl 的addUser()方法

@Override public void addUser(User u) {Session session = sessionFactory.openSession();Transaction tc = session.getTransaction();try { tc.begin(); session.save(u); tc.commit();}catch(Exception e){ tc.rollback(); e.printStackTrace();}return ; }

daos.xml 文件相應(yīng)配置

<bean class='org.springframework.orm.hibernate5.HibernateTransactionManager'><property name='sessionFactory' ref='mySessionFactory' /> </bean> <tx:annotation-driven transaction-manager='txManager' />

測(cè)試類(lèi)

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={'/services.xml'})@Transactional(transactionManager = 'txManager')@Rollback(true)public class UserServiceImplTest {@Autowired UserDAO userDAO; //自動(dòng)裝配userDAO@Test public void testAddUse(){User u = new User();u.setLevel(3);u.setName('ab11');u.setPassword('hh');userDAO.addUser(u);Assert.assertEquals(u.getName(), userDAO.getUserList().get(userDAO.getUserList().size()-1).getName()); }

部分控制臺(tái)打印信息

信息: Using DataSource [org.apache.commons.dbcp2.BasicDataSource@498d318c] of Hibernate SessionFactory for HibernateTransactionManager六月 02, 2017 4:46:19 下午 org.springframework.test.context.transaction.TransactionContext startTransaction信息: Began transaction (1) for test context [DefaultTestContext@52d6cd34 testClass = UserServiceImplTest, testInstance = com.dxzh.mall.serviceImpl.test.UserServiceImplTest@715d6168, testMethod = testAddUse@UserServiceImplTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@75798d03 testClass = UserServiceImplTest, locations = ’{classpath:/services.xml}’, classes = ’{}’, contextInitializerClasses = ’[]’, activeProfiles = ’{}’, propertySourceLocations = ’{}’, propertySourceProperties = ’{}’, contextCustomizers = set[[empty]], contextLoader = ’org.springframework.test.context.support.DelegatingSmartContextLoader’, parent = [null]]]; transaction manager [org.springframework.orm.hibernate5.HibernateTransactionManager@c6634d]; rollback [true]Fri Jun 02 16:46:19 CST 2017 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ’false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.Hibernate: insert into user (name, password, level) values (?, ?, ?)六月 02, 2017 4:46:19 下午 org.springframework.test.context.transaction.TransactionContext endTransaction信息: Rolled back transaction for test context [DefaultTestContext@52d6cd34 testClass = UserServiceImplTest, testInstance = com.dxzh.mall.serviceImpl.test.UserServiceImplTest@715d6168, testMethod = testAddUse@UserServiceImplTest, testException = java.lang.RuntimeException, mergedContextConfiguration = [MergedContextConfiguration@75798d03 testClass = UserServiceImplTest, locations = ’{classpath:/services.xml}’, classes = ’{}’, contextInitializerClasses = ’[]’, activeProfiles = ’{}’, propertySourceLocations = ’{}’, propertySourceProperties = ’{}’, contextCustomizers = set[[empty]], contextLoader = ’org.springframework.test.context.support.DelegatingSmartContextLoader’, parent = [null]]].六月 02, 2017 4:46:19 下午 org.springframework.context.support.GenericApplicationContext doClose信息: Closing org.springframework.context.support.GenericApplicationContext@3ffc5af1: startup date [Fri Jun 02 16:46:13 CST 2017]; root of context hierarchy

問(wèn)題解答

回答1:

用dbunit 結(jié)合 spring-test 去測(cè)試

回答2:

Transactional是service層事務(wù),用了就不必在DAO層寫(xiě)事務(wù)了

標(biāo)簽: java
相關(guān)文章:
主站蜘蛛池模板: 色婷婷色| 午夜毛片视频 | 无套内谢大学处破女www小说 | 欧美日韩99| 久久久久久黄色 | 日本一区二区精品视频 | 91免费国产视频 | 国内激情视频 | 亚洲在线免费观看视频 | 亚洲人体视频 | 日韩综合av | 亚洲不卡影院 | 国产精品中文字幕在线观看 | 我想看黄色大片 | 日产精品久久久久 | a级网站在线观看 | 啪啪日韩| 黄色av网| 天堂色在线 | 成人在线视频网址 | 台湾久久 | 最新国产在线视频 | 香蕉短视频 | 国产色图视频 | 亚洲一区 在线播放 | 成人免费在线网站 | 亚洲成人动漫在线观看 | av在线网页 | 一级片在线观看免费 | 色综合88| 在线观看日韩视频 | 成人黄色a| 美女av在线免费观看 | www欧美在线 | 97久久超碰 | 午夜天堂网 | 国产一区二区视频免费观看 | 色资源av| 手机午夜视频 | 爱情与灵药在线 | 婷婷五月在线视频 |