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

半腔热情 提交于 2019-12-10 09:27:18

问题


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));
    }
}

回答1:


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)


来源:https://stackoverflow.com/questions/34910981/how-can-i-prevent-my-spring-boot-batch-application-from-running-when-executing-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!