zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    【P000-004】交易费计算系统,功能类规划
    【P000-003】交易费计算系统,从股票信息网络接口获取信息
    ASP页面的执行顺序
    Python ImportError: DLL load failed: %1 不是有效的 Win32 应用程序
    VSCode运行已有代码
    WPF MVVM-TreeView数据源添加了节点,UI没有刷新
    MapGIS二次开发注意事项
    把echarts嵌入winform窗口注意事项
    host is not allowed to connect mysql解决方法
    SqlDbx连接Oracle数据库
  • 原文地址:https://www.cnblogs.com/zuiyue_jing/p/14507856.html
Copyright © 2011-2022 走看看