gpt4 book ai didi

spring-batch - 使用@Transactional(propagation = NOT_SUPPORTED)插入Spring Batch集成测试的测试记录不回滚

转载 作者:行者123 更新时间:2023-12-05 07:07:56 24 4
gpt4 key购买 nike

我想在测试我的 Spring Batch 作业时插入一条测试记录。

通常我会使用 @Transactional 注释测试类,但这不适用于使用 @SpringBatchTest 注释的测试类。

@SpringBatchTest
@SpringBootTest
@Transactional
public class JobTest {

@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;

@Autowired
private JdbcTemplate jdbcTemplate;

@Test
void someTest() throws Exception {
jdbcTemplate.update("insert into some_table (some_col1, some_col2) values ('foo', 'bar')");
jobLauncherTestUtils.launchJob();
}
}
Existing transaction detected in JobRepository. Please fix this and try again (e.g. remove @Transactional annotations from client).
java.lang.IllegalStateException: Existing transaction detected in JobRepository. Please fix this and try again (e.g. remove @Transactional annotations from client).
at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean$1.invoke(AbstractJobRepositoryFactoryBean.java:177)

我也试过@Transactional(propagation = NOT_SUPPORTED),但是记录没有回滚。这是 https://stackoverflow.com/a/46546171/10243546 中的建议

我只想要测试的测试记录,所以我不确定这个答案是否适用(https://stackoverflow.com/a/55001568/10243546),因为这只是一个测试。

最佳答案

您需要将测试设置移到测试方法之外,例如在使用 @Before (JUnit 4) 或 @BeforeEach (JUnit 5) 注释的方法中。像这样的东西:

@Before
public void initData() {
jdbcTemplate.update("insert into some_table (some_col1, some_col2) values ('foo', 'bar')");
}

@Test
void someTest() throws Exception {
jobLauncherTestUtils.launchJob();
}

关于spring-batch - 使用@Transactional(propagation = NOT_SUPPORTED)插入Spring Batch集成测试的测试记录不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62008246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com