Spring Batch End to End测试配置不起作用

问题描述 投票:0回答:1

我按照官方的Spring批量指南(https://spring.io/guides/gs/batch-processing/)成功地做了这个例子。对于同样的工作,我正在尝试创建端到端集成测试。特别是我只使用测试配置。在我的测试中,我正在定义所有需要的bean。所以这应该是运行作业所需的唯一配置。这提供了很大的灵活性。创建ApplicationContext时测试失败。它抱怨它无法找到数据源。当我运行实际的应用程序时,由于我使用的是内存数据库,因此spring会自动在数据库中创建数据源和spring批处理相关的表。但是,当我运行我的测试时,它不会自动创建数据库。如何触发弹簧来做到这一点?

在我的测试配置中,当我添加显式数据源bean配置时,它创建了数据源,但它没有在数据库中创建与spring批量相关的表。

以下是测试代码:

@SpringBatchTest
@RunWith(SpringRunner.class)
public class PersonJobTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void testPersonJob() throws Exception{

        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode());
    }


    @Configuration
    @EnableBatchProcessing
    public static class JobConfig {

        @Autowired
        public JobBuilderFactory jobBuilderFactory;

        @Autowired
        public StepBuilderFactory stepBuilderFactory;

        private JdbcTemplate simpleJdbcTemplate;

        @Bean
        public FlatFileItemReader<Person> reader() {
            return new FlatFileItemReaderBuilder<Person>()
                    .name("personItemReader")
                    .resource(new ClassPathResource("sample-data.csv"))
                    .delimited()
                    .names(new String[]{"firstName", "lastName"})
                    .fieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
                        setTargetType(Person.class);
                    }})
                    .build();
        }

        @Bean
        public PersonItemProcessor processor() {
            return new PersonItemProcessor();
        }

        @Bean
        @Autowired
        public JdbcBatchItemWriter<Person> writer(DataSource dataSource) {
            return new JdbcBatchItemWriterBuilder<Person>()
                    .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
                    .sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)")
                    .dataSource(dataSource)
                    .build();
        }

        @Bean
        public JdbcTemplate jdbcTemplate(DataSource dataSource) {
            return new JdbcTemplate(dataSource);
        }

        @Bean
        public JobCompletionNotificationListener getListener(JdbcTemplate jdbcTemplate){
            return new JobCompletionNotificationListener(jdbcTemplate);
        }

        @Bean
        public Job importUserJob(JobCompletionNotificationListener listener, Step step1) {
            return jobBuilderFactory.get("importUserJob")
                    .incrementer(new RunIdIncrementer())
                    .listener(listener)
                    .flow(step1)
                    .end()
                    .build();
        }

        @Bean
        public Step step1(JdbcBatchItemWriter<Person> writer) {
            return stepBuilderFactory.get("step1")
                    .<Person, Person> chunk(10)
                    .reader(reader())
                    .processor(processor())
                    .writer(writer)
                    .build();
        }
    }


}

pom.hml圆角:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.intuit.iip.dg.dgworkflow</groupId>
    <artifactId>dg-workflow-springbatch-poc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dg-workflow-springbatch-poc</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在运行测试时,我希望spring boot能够自动配置数据源,并创建与批处理相关的表,就像我运行实际应用程序时一样。

spring spring-boot spring-batch
1个回答
0
投票

您需要使用@SpringBootTest注释。此外,您无需在测试中复制作业配置。这是一个通过入门指南的测试:

import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.assertEquals;

@SpringBatchTest
@SpringBootTest
@RunWith(SpringRunner.class)
public class PersonJobTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void testPersonJob() throws Exception{
        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode());
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.