mercredi 24 février 2016

Testing Spring Batch application - batch runs fully before the test actually starts

I'm trying to create integration test for a Spring Boot batch application, following this article : http://ift.tt/1VDPSZ1

The problem is when I launch my test class with JUnit, the batch fully runs before the first instruction of the test method.

The entry point of the Spring Boot application :

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        ApplicationContext applicationContext = SpringApplication.run( Application.class, args );
        SpringApplication.exit( applicationContext );
    }

}

My main integration test class :

@RunWith(SpringJUnit4ClassRunner.class)
@IntegrationTest
@SpringApplicationConfiguration(classes = { TestJobConfiguration.class })
public class MyIntegrationTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void test() throws Exception {
        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next();

        // assertions...
    }
}

The configuration class for all tests :

@Configuration
@EnableAutoConfiguration
@Import({BatchConfiguration.class})
public class TestJobConfiguration {

    @Bean
    public JobLauncherTestUtils jobLauncherTestUtils() {
        return new JobLauncherTestUtils();
    }

}

BatchConfiguration is the class which defines all my beans : job, step, reader, processor, writer, etc.

I suppose, when Spring build the context for the integration test, it must come upon Application class, which runs the batch. But how can I prevent this happening ?

Aucun commentaire:

Enregistrer un commentaire