gpt4 book ai didi

使用 jobLauncherTestUtils 进行 Spring 批量集成测试

转载 作者:行者123 更新时间:2023-12-04 13:02:30 25 4
gpt4 key购买 nike

我正在尝试编写我的批处理项目的集成测试。

我的集成测试定义:

https://github.com/cristianprofile/spring-batch-testing/blob/master/src/test/java/test/BatchApplicationTests.java

@RunWith(SpringRunner.class)
@SpringBootTest

/* @Autowired
private JobLauncherTestUtils jobLauncherTestUtils;*/

public class BatchApplicationTests {

private static final String EXPECTED_FILE = "src/test/resources/users.csv";
private static final String OUTPUT_FILE = "src/main/resources//users.csv";
private static final String EXPECTED_FILE2 = "src/test/resources/users2.csv";
private static final String OUTPUT_FILE2 = "src/main/resources//users2.csv";

@Test
public void contextLoads() throws Exception{
System.out.println("Fake test ");
// JobExecution jobExecution = jobLauncherTestUtils.launchJob();
// JobExecution jobExecution = jobLauncherTestUtils.launchStep("step1");
// Assert.assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode());
//
//
// AssertFile.assertFileEquals(new FileSystemResource(EXPECTED_FILE),
// new FileSystemResource(OUTPUT_FILE));
//
// AssertFile.assertFileEquals(new FileSystemResource(EXPECTED_FILE2),
// new FileSystemResource(OUTPUT_FILE2));

}

Spring配置测试文件夹:
package test;

import com.mymoid.batch.configuration.BatchConfiguration;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Import;

@SpringBootConfiguration
@EnableAutoConfiguration
@Import({BatchTestConfig.class, BatchConfiguration.class})
public class TestServiceConfigIT {
}

BatchTestConfig 类:
@Configuration
public class BatchTestConfig {

@Bean
JobLauncherTestUtils jobLauncherTestUtils() {
return new JobLauncherTestUtils();
}

}

BatchConfiguration.class:
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Autowired
public DataSource dataSource;

@Bean
public JdbcCursorItemReader<User> reader(){
JdbcCursorItemReader<User> reader = new JdbcCursorItemReader<User>();
reader.setDataSource(dataSource);
reader.setSql("SELECT id,name FROM user");
reader.setRowMapper(new UserRowMapper());
return reader;
}



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

@Bean
public FlatFileItemWriter<User> writer(){
FlatFileItemWriter<User> writer = new FlatFileItemWriter<User>();

final FileSystemResource fileSystemResource = new FileSystemResource("/Users/mymoid/projects/batch/src/main" +
"/resources/users.csv");

FlatFileHeaderCallback prueba= new FlatFileHeaderCallback() {
@Override
public void writeHeader(Writer writer) throws IOException {
writer.write("columna1,cloumna2");
}
};
writer.setHeaderCallback(prueba);
writer.setResource(fileSystemResource);
writer.setLineAggregator(new DelimitedLineAggregator<User>() {{
setDelimiter(",");
setFieldExtractor(new BeanWrapperFieldExtractor<User>() {{
setNames(new String[] { "address.id", "address.name" });
}});
}});

return writer;
}


@Bean
public FlatFileItemWriter<User> writer2(){
FlatFileItemWriter<User> writer = new FlatFileItemWriter<User>();
final FileSystemResource fileSystemResource = new FileSystemResource("/Users/mymoid/projects/batch/src/main" +
"/resources/users2.csv");

FlatFileHeaderCallback prueba= new FlatFileHeaderCallback() {
@Override
public void writeHeader(Writer writer) throws IOException {
writer.write("columna1");
}
};
writer.setHeaderCallback(prueba);
writer.setResource(fileSystemResource);
writer.setLineAggregator(new DelimitedLineAggregator<User>() {{
setDelimiter(",");
setFieldExtractor(new BeanWrapperFieldExtractor<User>() {{
setNames(new String[] { "id"});
}});
}});

return writer;
}




@Bean
public CompositeItemWriter compositeItemWritercomposite(){
FlatFileItemWriter<User> writer = new FlatFileItemWriter<User>();
writer.setResource(new ClassPathResource("users2.csv"));
writer.setLineAggregator(new DelimitedLineAggregator<User>() {{
setDelimiter(",");
setFieldExtractor(new BeanWrapperFieldExtractor<User>() {{
setNames(new String[] { "id"});
}});
}});

final FlatFileItemWriter writer1 = writer();
final FlatFileItemWriter<User> userFlatFileItemWriter = writer2();
List<ItemWriter<User>> writers = new ArrayList<>(2);
writers.add(writer1);
writers.add(userFlatFileItemWriter);
CompositeItemWriter itemWriter = new CompositeItemWriter();

itemWriter.setDelegates(writers);
return itemWriter;
}


@Bean OrderWriter orderWriter()
{
return new OrderWriter();
}







@Bean
public Step step1() {
return stepBuilderFactory.get("step1").<User, User> chunk(2)
.reader(reader())
.processor(processor())
.writer(compositeItemWritercomposite())
.build();
}

@Bean
public Job exportUserJob() {
return jobBuilderFactory.get("exportUserJob")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}

}

当我运行 contextLoads(测试用例为空)时,Spring 的运行方式与我运行主程序时的运行方式相同。如果我的代码的每一行都被注释了,为什么 Spring 会运行批处理?

enter image description here

最佳答案

来自 SpringBootTest注释的javadoc :

Automatically searches for a @SpringBootConfiguration when nested @Configuration is not used, and no explicit classes are specified.


在你的测试类中,你没有使用 @contextConfiguration或指定从哪些类加载应用程序上下文。因此 Spring Boot 会自动搜索 Spring Boot 配置,它会找到您的批处理作业配置并运行它。
您可以通过添加 spring.batch.job.enabled=false 来禁用它给您的 application.properties文件。

关于使用 jobLauncherTestUtils 进行 Spring 批量集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52474848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com