Spring错误——JUnit事务自动回滚

背景

  • 在Spring 单元测试中,使用的是JUnit4进行测试。测试过程不报错,但是数据不能插入到数据库中。
  • 测试代码如下
@Test
@Transactional
public void saveElec() {
    elecService.saveElec(elecEntity);
    ElecEntity temp_elec = elecService.getElecById(elec.getId());
    Assert.assertNotNull("插入数据库失败", temp_elec);
}

日志

三月 09, 2021 13:11:22 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
三月 09, 2021 13:11:22 下午 org.springframework.orm.hibernate5.HibernateTransactionManager afterPropertiesSet
信息: Using DataSource [org.apache.commons.dbcp2.BasicDataSource@5656be13] of Hibernate SessionFactory for HibernateTransactionManager
三月 09, 2021 4:31:33 下午 org.springframework.test.context.transaction.TransactionContext startTransaction
信息: Began transaction (1) for test context [DefaultTestContext@41fed14f testClass = UserServiceImplTest, testInstance = com.ths.demo3.service.serviceImpl.UserServiceImplTest@4d6ee47, testMethod = saveUser@UserServiceImplTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@6818d900 testClass = UserServiceImplTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[org.springframework.test.context.web.socket.MockServerContainerContextCustomizer@6442b0a6], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.orm.hibernate5.HibernateTransactionManager@2c306a57]; rollback [true]
三月 09, 2021 4:31:33 下午 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
三月 09, 2021 4:31:33 下午 org.hibernate.hql.internal.ast.HqlSqlWalker generatePositionalParameter
WARN: [DEPRECATION] Encountered positional parameter near line 1, column 41 in HQL: [from com.ths.demo3.vo.User u where u.id=?]. Positional parameter are considered deprecated; use named parameters or JPA-style positional parameters instead.
三月 09, 2021 4:31:33 下午 org.springframework.test.context.transaction.TransactionContext endTransaction
信息: Rolled back transaction for test: [DefaultTestContext@41fed14f testClass = UserServiceImplTest, testInstance = com.ths.demo3.service.serviceImpl.UserServiceImplTest@4d6ee47, testMethod = saveUser@UserServiceImplTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@6818d900 testClass = UserServiceImplTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[org.springframework.test.context.web.socket.MockServerContainerContextCustomizer@6442b0a6], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]]

原因

  • JUnit环境@Transactional注解下,存在注解@Rollback,表示对事物进行操作,默认为true

解决方法

  • 测试方法加上注解 @Rollback(true)
posted @ 2021-03-09 20:21  话·醉月  阅读(822)  评论(0编辑  收藏  举报