如何保存测试数据以供 spring 批集成测试使用
How to save test data to be consumed by a spring batch integration test
我正在尝试使用我的 JPA 存储库,以便 将测试数据保存到 h2 中,然后供 spring 批处理集成测试使用 。
这是我的集成测试:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = Batch.class)
public class MessageDigestMailingStepIT extends AbstractBatchIntegrationTest {
@Autowired
@Qualifier("messagesDigestMailingJob")
private Job messagesDigestMailingJob;
@Autowired
private JobLauncher jobLauncher;
@Autowired
private JobRepository jobRepository;
@Autowired
private UserAccountRepository userAccountRepository;
@Autowired
private MessageRepository messageRepository;
private JobLauncherTestUtils jobLauncherTestUtils;
@Before
public void setUp() {
this.jobLauncherTestUtils = new JobLauncherTestUtils();
this.jobLauncherTestUtils.setJobLauncher(jobLauncher);
this.jobLauncherTestUtils.setJobRepository(jobRepository);
this.jobLauncherTestUtils.setJob(messagesDigestMailingJob);
}
@Test
@Transactional
public void shouldSendMessageDigestAndUpdateNotificationSent() {
UserAccount userAccount = DomainFactory.createUserAccount("me@example.com");
userAccountRepository.save(userAccount);
JobParameters jobParameters = new JobParametersBuilder().addDate("execution_date", new Date()).toJobParameters();
jobLauncherTestUtils.launchStep("messagesDigestMailingStep", jobParameters);
//Assertions
}
}
注意测试方法上的@Transactional
。不幸的是 Spring 批处理使用它自己的事务,而我对 @Transactional
的使用与 spring 批处理事务发生冲突。
这是我收到的错误消息:
java.lang.IllegalStateException: Existing transaction detected in JobRepository. Please fix this and try again (e.g. remove @Transactional annotations from client).
有人可以建议如何插入测试数据以供 spring 批量集成测试使用吗?
edit:为了更好的衡量,这里是 AbstractBatchIntegrationTest
class:
的定义
@AutoConfigureTestEntityManager
@AutoConfigureJson
@AutoConfigureJsonTesters
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles(Profiles.TEST)
@ComponentScan(basePackages = {"com.bignibou.it.configuration", "com.bignibou.configuration"})
public abstract class AbstractBatchIntegrationTest {
}
编辑:我决定只依赖@Sql
注解如下:
@Sql(scripts = "insert_message.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "clean_database.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
@Test
public void shouldSendMessageDigestAndUpdateNotificationSent() {
...
从测试中删除 @Transactional
,以便 UserAccount
立即保存到数据库中。然后使用 @Sql
和 ExecutionPhase.AFTER_TEST_METHOD
执行清理脚本(或 内联 语句)以手动撤消测试期间执行的更改。
我正在尝试使用我的 JPA 存储库,以便 将测试数据保存到 h2 中,然后供 spring 批处理集成测试使用 。
这是我的集成测试:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = Batch.class)
public class MessageDigestMailingStepIT extends AbstractBatchIntegrationTest {
@Autowired
@Qualifier("messagesDigestMailingJob")
private Job messagesDigestMailingJob;
@Autowired
private JobLauncher jobLauncher;
@Autowired
private JobRepository jobRepository;
@Autowired
private UserAccountRepository userAccountRepository;
@Autowired
private MessageRepository messageRepository;
private JobLauncherTestUtils jobLauncherTestUtils;
@Before
public void setUp() {
this.jobLauncherTestUtils = new JobLauncherTestUtils();
this.jobLauncherTestUtils.setJobLauncher(jobLauncher);
this.jobLauncherTestUtils.setJobRepository(jobRepository);
this.jobLauncherTestUtils.setJob(messagesDigestMailingJob);
}
@Test
@Transactional
public void shouldSendMessageDigestAndUpdateNotificationSent() {
UserAccount userAccount = DomainFactory.createUserAccount("me@example.com");
userAccountRepository.save(userAccount);
JobParameters jobParameters = new JobParametersBuilder().addDate("execution_date", new Date()).toJobParameters();
jobLauncherTestUtils.launchStep("messagesDigestMailingStep", jobParameters);
//Assertions
}
}
注意测试方法上的@Transactional
。不幸的是 Spring 批处理使用它自己的事务,而我对 @Transactional
的使用与 spring 批处理事务发生冲突。
这是我收到的错误消息:
java.lang.IllegalStateException: Existing transaction detected in JobRepository. Please fix this and try again (e.g. remove @Transactional annotations from client).
有人可以建议如何插入测试数据以供 spring 批量集成测试使用吗?
edit:为了更好的衡量,这里是 AbstractBatchIntegrationTest
class:
@AutoConfigureTestEntityManager
@AutoConfigureJson
@AutoConfigureJsonTesters
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles(Profiles.TEST)
@ComponentScan(basePackages = {"com.bignibou.it.configuration", "com.bignibou.configuration"})
public abstract class AbstractBatchIntegrationTest {
}
编辑:我决定只依赖@Sql
注解如下:
@Sql(scripts = "insert_message.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "clean_database.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
@Test
public void shouldSendMessageDigestAndUpdateNotificationSent() {
...
从测试中删除 @Transactional
,以便 UserAccount
立即保存到数据库中。然后使用 @Sql
和 ExecutionPhase.AFTER_TEST_METHOD
执行清理脚本(或 内联 语句)以手动撤消测试期间执行的更改。