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

How to run @SpringBatchTest with junit5 jupiter engine

我在下面 link 编写端到端 spring 批处理作业测试。

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

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

由于我的其他非批处理相关测试用例 运行 在 jupiter 引擎上,我想在端到端 spring 批处理作业测试中使用相同的测试用例。如果我将 @RunWith(SpringRunner.class) 替换为 @ExtendWith(SpringExtension.class),我可以看到 none 个自动装配字段已实例化并具有空值。

由于我是 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());

        }
    }

}

你可以去掉 @RunWith(SpringRunner.class)。不需要添加 @ExtendWith(SpringExtension.class) 因为 @SpringBootTest 已经添加了 - 至少在当前版本的 Spring Boot.

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

import org.junit.Test;

进入

import org.junit.jupiter.api.Test;

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