
[英]Spring Integration: javax.persistence.TransactionRequiredException: no transaction is in progress
[英]javax.persistence.TransactionRequiredException in Spring Boot integration test
我有一个Spring Boot 2应用程序,其中有一个JdbcPersistenceHelper
,如下所示:
@Component
public class JdbcPersistenceHelper {
private EntityManager entityManager;
@Autowired
public JdbcPersistenceHelper(EntityManager entityManager) {
this.entityManager = entityManager;
}
// Some persistence-related utility methods here
我正在使用此类从测试中生成一些夹具数据,并且有几个成功使用它的@DataJpaTest
注释测试。 但是,当我尝试在集成测试(带有@SpringBootTest
注释的测试类)中使用它时,出现以下错误:
javax.persistence.TransactionRequiredException:执行更新/删除查询
我的集成测试如下所示:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = ExchangeRateStoreApplication.class)
@AutoConfigureMockMvc
public class GenerateStrategyRatesTest {
@Autowired
MockMvc mockMvc;
@Autowired
EntityManager entityManager;
@Autowired
JdbcPersistenceHelper jdbcPersistenceHelper;
@BeforeEach
void setUp() {
jdbcPersistenceHelper.persistAllUpdatingStrategiesAvailable();
jdbcPersistenceHelper.persistAllExchangeRateProvidersAvailable();
}
// Some test methods here
目前,我正在使用H2内存数据库,而Spring Boot的application.yaml
如下所示:
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:ExchangeRateDB;DB_CLOSE_DELAY=-1
username: err
jpa:
database-platform: org.hibernate.dialect.H2Dialect
我想念什么吗?
标记您将测试类或before方法绑定到事务:
@BeforeEach
@Transactional
void setUp() {
要么
@SpringBootTest
@Transactional
@AutoConfigureMockMvc
public class GenerateStrategyRatesTest
如果没有正在运行的事务,则无法执行持久/更新/删除操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.