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)
  • 相关阅读:
    Golang gRPC 入门
    ProtoBuf编解码
    Proto3语法入门
    Golang RPC入门
    Windows用mstsc(远程桌面)远程Ubuntu 12.04时无法显示Ubuntu桌面解决办法
    ABAPSM30配置TCODE
    eclipselog4j.properties配置
    Hadoop—配置3个副本,但是实际只有1个副本。
    Hadoop—org.apache.hadoop.io.nativeio.NativeIO$Windows.access0
    Jmeter BeanShell使用场景
  • 原文地址:https://www.cnblogs.com/zuiyue_jing/p/14507856.html
Copyright © 2011-2022 走看看