如何使用junit5 jupiter引擎运行@SpringBatchTest

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

我按照下面的链接编写端到端春季批处理作业测试。

https://docs.spring.io/spring-batch/docs/current/reference/html/testing.html#endToEndTesting

这告诉您使用 @RunWith(SpringRunner.class) 注释您的测试类,这是 Junit4 功能,并且我的测试用例使用 junit5 的老式引擎运行良好。

由于我的其他非批处理相关测试用例正在 jupiter 引擎上运行,因此我想使用相同的方法进行端到端 Spring 批处理作业测试。如果我将 @RunWith(SpringRunner.class) 替换为 @ExtendWith(SpringExtension.class),我可以看到没有任何自动装配字段被实例化并且具有空值。

由于我对 Spring Batch 和 Jupiter 都是新手,我无法理解出了什么问题。

任何指针将不胜感激

junit测试用例示例代码

import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.JobRepositoryTestUtils;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.assertj.core.api.Assertions.assertThat;

@Slf4j
@Testcontainers
@SpringBatchTest
@SpringBootTest
@RunWith(SpringRunner.class)
//@ExtendWith(SpringExtension.class)
@ContextConfiguration(initializers = {BatchJobConfigTest.Initializer.class})
@Import({LiquibaseConfigprimary.class, LiquibaseConfigsecondary.class})
public class BatchJobConfigTest {

    @Container
    private static final PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:latest")
            .withDatabaseName("dummy-db")
            .withUsername("sa")
            .withPassword("sa");

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;
    @Autowired
    @Qualifier("primaryDatasource")
    private HikariDataSource primaryDatasource;
    @Autowired
    @Qualifier("secondaryDatasource")
    private HikariDataSource secondaryDatasource;
    @Autowired
    private SecondaryRepository secondaryRepository;   
    @Autowired
    private PrimaryRepository primaryRepository;
    @Autowired
    private JobRepositoryTestUtils jobRepositoryTestUtils;
    @Autowired
    private BatchConfigurer batchConfigurer;
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @BeforeEach
    void setup() {
        secondaryRepository.deleteAll();
        primaryRepository.deleteAll();
    }

    @Test
    public void testJob() throws Exception {
        assertThat(primaryRepository.findAll()).hasSize(1);
        assertThat(secondaryRepository.findAll()).hasSize(0);
        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        assertThat(secondaryRepository.findAll()).hasSize(1);
        assertThat(jobExecution.getExitStatus().getExitCode()).isEqualTo("COMPLETED");
    }

    static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
            if (!postgreSQLContainer.isRunning())
                postgreSQLContainer.start();
            TestPropertyValues.of(
                    "spring.data.postgres.url=" + postgreSQLContainer.getJdbcUrl(),
                    "spring.data.postgres.username=" + postgreSQLContainer.getUsername(),
                    "spring.data.postgres.password=" + postgreSQLContainer.getPassword(),
                    "spring.data.postgres.host=localhost",
                    "spring.data.postgres.port=" + postgreSQLContainer.getFirstMappedPort(),
                    "spring.data.postgres.database=" + postgreSQLContainer.getDatabaseName()
            ).applyTo(configurableApplicationContext.getEnvironment());

            TestPropertyValues.of(
                    "spring.datasource-secondary.jdbc-url=" + postgreSQLContainer.getJdbcUrl(),
                    "spring.datasource-secondary.username=" + postgreSQLContainer.getUsername(),
                    "spring.datasource-secondary.password=" + postgreSQLContainer.getPassword(),
                    "spring.datasource-secondary.driver-class-name=org.postgresql.Driver"
            ).applyTo(configurableApplicationContext.getEnvironment());

        }
    }

}
spring-boot spring-batch junit5
1个回答
7
投票

你可以摆脱

@RunWith(SpringRunner.class)
。无需添加
@ExtendWith(SpringExtension.class)
,因为它已经由
@SpringBootTest
添加了 - 至少在当前版本的 Spring Boot 中是这样。

接下来你要做的就是改变:

import org.junit.Test;

进入

import org.junit.jupiter.api.Test;

因为这就是告诉 JUnit 平台运行 Jupiter 测试而不是 Vintage 测试的原因。希望能解决您的问题。

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