• 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)
  • 相关阅读:
    windows 服务中托管asp.net core
    asp.net core自定义端口
    asp.net core 2.1 部署IIS(win10/win7)
    Centos7 安装Tomcat并运行程序
    centos7 安装java运行环境
    linux(centos7) 常用命令和快捷键 持续验证更新中...
    CentOS7 设置yum源
    dotnet core 入门命令
    二项式系数学习笔记
    [容斥原理][莫比乌斯反演] Codeforces 803F Coprime Subsequences
  • 原文地址:https://www.cnblogs.com/zuiyue_jing/p/14507856.html
Copyright © 2020-2023  润新知