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));
}
}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With