使用Mockito进行Spring批处理作业测试

2uluyalo  于 2023-10-18  发布在  Spring
关注(0)|答案(1)|浏览(158)

我是新来的 Spring 批申请。我正在使用Junit和mockito为Spring批处理作业编写测试用例。我试图使用内存数据库编写测试用例,但我们使用的是一些Oracle特定的函数,因此无法在Oracle H2 DB中使用,因此决定使用mockito来模拟DB调用。
我的批处理作业是从DB中阅读&处理数据&写回DB。
jobConfig.java

private final JdbcCursorItemReader<LimsExternalLinkDTO> findExternalLinksReader;

@Bean(name = VALIDATE_EXTERNAL_LINK_JOB)
public Job jobValidateExternalLinks() {
    return jobBuilderFactory.get(VALIDATE_EXTERNAL_LINK_JOB)
            .incrementer(new RunIdIncrementer())
            .start(stepValidateExternalLinks())
            .build();
}

@Bean
public Step stepValidateExternalLinks() {
    return stepBuilderFactory.get("stepValidateExternalLinks")
            .<LimsExternalLinkDTO, LimsExternalLinkDTO> chunk(VALIDATE_LINKS_BATCH_SIZE)
            .reader(validateExternalLinksReader)
            .processor(validateExternalLinksProcessor)
            .writer(jdbcBatchItemWriterValidateExternalLink)
            .build();
}

Reader.java

@Bean
     @StepScope
        public JdbcCursorItemReader<LimsExternalLinkDTO> validateExternalLinksReader(){
            JdbcCursorItemReader<LimsExternalLinkDTO> reader = new JdbcCursorItemReader<>();
            reader.setDataSource(dataSource);
            reader.setSql(GET_LINK_WHERE_STATUS_NULL);
            reader.setRowMapper(new BeanPropertyRowMapper<>(LimsExternalLinkDTO.class));
            reader.setFetchSize(BATCH_SIZE);
            reader.setQueryTimeout(REQUEST_TIMEOUT);
            return reader;
        }

writer.java

@Bean
public JdbcBatchItemWriter<LimsExternalLinkDTO> jdbcBatchItemWriterValidateExternalLink(){
    JdbcBatchItemWriter<LimsExternalLinkDTO> jdbcBatchItemWriter = new JdbcBatchItemWriter<>();
    jdbcBatchItemWriter.setDataSource(dataSource);
    jdbcBatchItemWriter.setSql(UPDATE_LIMS_EXTERNAL_LINKS_QUERY);
    jdbcBatchItemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    return jdbcBatchItemWriter;
}

processor.java
private final LinkValidateService;

@Bean
public ItemProcessor<LimsExternalLinkDTO, LimsExternalLinkDTO> validateExternalLinksProcessor(){
    return linkData -> {
        System.out.println("processed data :"+linkData);
        linkData.setStatus(linkValidateService.validateLink(linkData.getExternalLink()) ? "VALID" : "INVALID");
        return linkData;
    };
}

jobConfigTest.java我在jobconfig类中有多个作业

@SpringBootTest
@ContextConfiguration(classes= BatchStepTest.TestConfig.class)
//@SpringBatchTest
@SpringJUnitConfig({LimsReportApplication.class, JobConfiguration.class})
public class BatchStepTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Autowired
    private JobRepositoryTestUtils jobRepositoryTestUtils;

    @BeforeEach
    void setUp() {

        jobRepositoryTestUtils.removeJobExecutions();
        Mockito.when()
    }

    @AfterEach
    void tearDown() {
    }

    @Test
    void testSTep() throws Exception {

        //WHEN
        int bookId = 316;
        JobParameters jobParameters = new JobParametersBuilder().addString("bookId", String.valueOf(bookId))
                .toJobParameters();
        JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobParameters);

        //THEN
        Assertions.assertEquals(ExitStatus.COMPLETED,jobExecution.getExitStatus());
    }

    @TestConfiguration
    static class TestConfig extends BatchJobTestConfiguration {
        @Override
        @Bean
        JobLauncherTestUtils getJobLauncherTestUtils() {
            return new JobLauncherTestUtils() {
                @Override
                @Autowired
                public void setJob(@Qualifier("jobValidateExternalLinks") Job job) {
                    super.setJob(job);
                }
            };
        }
    }
}

上面的测试调用实际的批处理作业,并执行DB调用。如何模拟DB调用并测试批处理作业?请帮

2w3rbyxf

2w3rbyxf1#

上面的测试调用实际的批处理作业,并执行DB调用。如何模拟DB调用并测试批处理作业?
这是因为测试使用的是Sping Boot 在测试中自动配置的同一个配置。如果你真的需要模拟数据库调用,你需要定义一个mock对象并模拟所有对它的调用(比如getConnection()等)。
仅供参考,Oracle为开发人员提供了一个免费版本,您可以用于测试:https://www.oracle.com/database/free/get-started/#installing。我会用那个版本创建一个测试程序。

相关问题