jeudi 22 octobre 2020

Spring Batch - Rollback while JUnit Test

I would like to ask you about rollback transaction while integration testing SpringBatch. I would like to achieve sth like this:

  1. In JUnit run job which do some active operation on database (insert, update etc).
  2. After test, rollback all change from database

I have test class like this:

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBatchTest
@SpringBootTest
@EnableAutoConfiguration
@ContextConfiguration(classes = {
  JobConfig.class,
  PersistenceConfig.class
})
@TestExecutionListeners({
  DependencyInjectionTestExecutionListener.class,
  DirtiesContextTestExecutionListener.class
})
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class SpringBatchIntegrationTest implements EnumConverter {

  @Autowired
  private JobLauncherTestUtils jobLauncherTestUtils;

  @Autowired
  private JobRepositoryTestUtils jobRepositoryTestUtils;

  @Before
  public void cleanUp() {
    jobRepositoryTestUtils.removeJobExecutions();
  }

And my test run job:

final JobExecution jobExecution = jobLauncherTestUtils.launchJob(defaultJobParameters());

final JobInstance actualJobInstance = jobExecution.getJobInstance();
final ExitStatus actualJobExitStatus = jobExecution.getExitStatus();

Is it possible to do this? I was looking for solution but I didn't find anything. @Transactional is deprecated and not working in SpringBatch. I also try to add:

@Test @Sql(scripts = "/sql/clear.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) public void givenReferenceOutputWhenJobExecutedThenSuccess() throws Exception {

but my clear.sql isn't executed.

Aucun commentaire:

Enregistrer un commentaire