Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent my Spring Boot Batch application from running when executing test?

I have a Spring Boot Batch application that I'm writing integration tests against. When I execute a test, the entire batch application runs. How can I execute just the application code under test?

Here's my test code. When it executes, the entire batch job step runs (reader, processor, and writer). Then, the test runs.

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BatchApplication.class))
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
        StepScopeTestExecutionListener.class })
public class StepScopeTestExecutionListenerIntegrationTests {

    @Autowired
    private FlatFileItemReader<String> reader;

    @Rule
    public TemporaryFolder testFolder = new TemporaryFolder();

    public StepExecution getStepExection() {
        StepExecution execution = MetaDataInstanceFactory.createStepExecution();
        return execution;
    }

    @Test
    public void testGoodData() throws Exception {
       //some test code on one met
       File testFile = testFolder.newFile();

       PrintWriter writer = new PrintWriter(testFile, "UTF-8");
       writer.println("test");
       writer.close();
       reader.setResource(new FileSystemResource(testFile));
       reader.open(getStepExection().getExecutionContext());
       String test = reader.read();
       reader.close();
       assertThat("test", equalTo(test));
    }
}
like image 725
James Avatar asked Jan 20 '16 21:01

James


1 Answers

Try to create application.properties file in test resources (e.g. src/main/resources) with this content:

spring.batch.job.enabled=false

You need to make sure that integration test reads. For that you may need to use ConfigFileApplicationContextInitializer this way:

@SpringApplicationConfiguration(classes = TestApplication.class, 
         initializers = ConfigFileApplicationContextInitializer.class)
like image 123
luboskrnac Avatar answered Oct 12 '22 23:10

luboskrnac