Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring批处理端到端测试配置不工作_Spring_Spring Boot_Spring Batch - Fatal编程技术网

Spring批处理端到端测试配置不工作

Spring批处理端到端测试配置不工作,spring,spring-boot,spring-batch,Spring,Spring Boot,Spring Batch,我遵循了官方的Spring批处理指南()并成功地完成了示例。对于相同的工作,我尝试创建一个端到端集成测试。特别是我只使用测试配置。在我的测试中,我定义了所有需要的bean。因此,这应该是运行作业所需的唯一配置。这提供了很大的灵活性。创建ApplicationContext时测试失败。它正在抱怨找不到数据源。当我运行实际的应用程序时,因为我使用的是内存中的数据库,所以spring会自动在数据库中创建数据源和spring批处理相关的表。但当我运行测试时,它不会自动创建数据库。我怎样才能触发sprin

我遵循了官方的Spring批处理指南()并成功地完成了示例。对于相同的工作,我尝试创建一个端到端集成测试。特别是我只使用测试配置。在我的测试中,我定义了所有需要的bean。因此,这应该是运行作业所需的唯一配置。这提供了很大的灵活性。创建ApplicationContext时测试失败。它正在抱怨找不到数据源。当我运行实际的应用程序时,因为我使用的是内存中的数据库,所以spring会自动在数据库中创建数据源和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();
        }
    }


}
@SpringBatchTest
@RunWith(SpringRunner.class)
公开课人员职务考试{
@自动连线
私有JobLauncherTestUtils JobLauncherTestUtils;
@试验
public void testPersonJob()引发异常{
JobExecution JobExecution=JobLaunchTestUtils.launchJob();
assertEquals(“已完成”,jobExecution.getExitStatus().getExitCode());
}
@配置
@启用批处理
公共静态类JobConfig{
@自动连线
公共建筑商建筑商工厂;
@自动连线
公共StepBuilderFactory StepBuilderFactory;
私有jdbc模板simpleJdbcTemplate;
@豆子
公共FlatFileItemReader()读取器{
返回新的FlatFileItemReaderBuilder()
.名称(“personItemReader”)
.resource(新类路径资源(“sample data.csv”))
.delimited()
.names(新字符串[]{“firstName”,“lastName”})
.fieldSetMapper(新的BeanRapperFieldSetMapper(){{
setTargetType(Person.class);
}})
.build();
}
@豆子
公共PersonItemProcessor处理器(){
返回新的PersonItemProcessor();
}
@豆子
@自动连线
公共JdbcBatchItemWriter编写器(数据源数据源){
返回新的JdbcBatchItemWriterBuilder()
.itemSqlParameterSourceProvider(新的BeanPropertyItemSqlParameterSourceProvider())
.sql(“插入到人员(名字、姓氏)值(:firstName,:lastName)”)
.dataSource(数据源)
.build();
}
@豆子
公共JdbcTemplate JdbcTemplate(数据源数据源){
返回新的JdbcTemplate(数据源);
}
@豆子
public Job CompletionNotificationListener getListener(JdbcTemplate JdbcTemplate){
返回新的JobCompletionNotificationListener(jdbcTemplate);
}
@豆子
公共作业导入器作业(作业完成通知侦听器侦听器,步骤1){
返回jobBuilderFactory.get(“importUserJob”)
.incrementer(新的RunIdIncrementer())
.listener(侦听器)
.流程(步骤1)
(完)
.build();
}
@豆子
公共步骤步骤1(JdbcBatchItemWriter编写器){
返回stepBuilderFactory.get(“step1”)
.chunk(10)
.reader(reader())
.processor(处理器())
.作者(作者)
.build();
}
}
}
pom.xml文件:

<?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>

4.0.0
org.springframework.boot
spring启动程序父级
2.1.4.1发布
com.intuit.iip.dg.dg工作流
dg工作流springbatch poc
0.0.1-快照
dg工作流springbatch poc
SpringBoot的演示项目
1.8
org.springframework.boot
弹簧靴起动器批次
org.hsqldb
hsqldb
org.springframework.boot
弹簧起动试验
测试
org.springframework.batch
弹簧批量试验
测试
org.springframework.boot
springbootmaven插件

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

您需要使用
@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());
    }

}

为此,您需要使用
@springbootest
注释。此外,您不需要在测试中复制作业配置。以下是通过《入门指南》的测试:

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

}

伟大的“很高兴它有帮助。”马哈茂德本哈辛这样说。我们不需要使用SpringBootTest注释。你怎么看?问题是关于Spring Boot的:
我希望Spring Boot自动配置数据源,并创建Spring批处理相关表
,因此我的答案是
@SpringBootTest
。您链接的文档适用于在没有Spring Boot的情况下使用Spring批处理的情况,在这种情况下,您不需要
@SpringBootTest
。太好了!“很高兴它有帮助。”马哈茂德本哈辛这样说。我们不需要使用SpringBootTest注释。你怎么看?问题是关于Spring Boot的:
我希望Spring Boot自动配置数据源,并创建Spring批处理相关表
,因此我的答案是
@SpringBootTest
。您链接的文档