如何在执行测试时阻止我的Spring Boot Batch应用程序运行?

时间:2016-01-20 21:31:34

标签: java spring spring-boot spring-batch spring-test

我有一个Spring Boot Batch应用程序,我正在编写集成测试。当我执行测试时,整个批处理应用程序运行。如何只执行测试中的应用程序代码?

这是我的测试代码。执行时,将运行整个批处理作业步骤(读取器,处理器和编写器)。然后,测试运行。

@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 个答案:

答案 0 :(得分:5)

尝试使用以下内容在测试资源(例如src / main / resources)中创建application.properties文件:

spring.batch.job.enabled=false

您需要确保集成测试读取。为此,您可能需要以这种方式使用ConfigFileApplicationContextInitializer

@SpringApplicationConfiguration(classes = TestApplication.class, 
         initializers = ConfigFileApplicationContextInitializer.class)