maven springboot 测试未运行特定数据源 bean 的集成测试

发布于 2025-01-20 13:35:03 字数 4984 浏览 0 评论 0原文

在春季测试中,我已经挣扎了几天。 我们正在使用Maven-Failsafe-Plugin,并在我们的两个特定数据库中使用Docker容器。

但是,当我们包括非主要数据源配置时,没有运行测试。

因此,配置是(Docker-Maven-Plugin为我们设置IP/主机系统属性):

metadata:
  datasource:
    url: jdbc:mariadb://${docker.mariadb.ip}:${docker.mariadb.port}/METADATA
    password: m_user
    username: m_pswd

reporting:
  datasource:
    jdbcUrl: jdbc:vertica://${docker.vertica.ip}:${docker.vertica.port}/REPORTING?allowMultiQueries=true
    username: v_user
    password: v_pswd

我们有两个数据库配置类。

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactory",
        basePackages = {"com.company.application"}
)
public class MariadbConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "metadata.datasource")
    public DataSourceProperties reportingDataSourceProperties() {
        return new DataSourceProperties();
    }


    @Primary
    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "metadata.datasource")
    public HikariDataSource dataSource(DataSourceProperties mariadbDataSourceProperties) {
        return (HikariDataSource) mariadbDataSourceProperties.initializeDataSourceBuilder()
                .type(HikariDataSource.class).build();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
        ...
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory")
            EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

    @Primary
    @Bean(name = "transactionTemplate")
    public TransactionTemplate transactionTemplate(@Qualifier("transactionManager")
            PlatformTransactionManager transactionManager) {
        return new TransactionTemplate(transactionManager);
    }
}

因此,

@Configuration
public class VerticaConfiguration {

    @Bean("verticaDataSourceProperties")
    @ConfigurationProperties(prefix = "reporting.datasource")
    public DataSourceProperties verticaDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "verticaDataSource")
    @ConfigurationProperties(prefix = "reporting.datasource")
    public HikariDataSource verticaDataSource() {
        return (HikariDataSource) verticaDataSourceProperties().initializeDataSourceBuilder()
                .type(HikariDataSource.class).build();
    }

    @Bean(name = "verticaJdbcTemplate")
    public JdbcTemplate verticaJdbcTemplate() {
        return new JdbcTemplate(verticaDataSource());
    }
}

我们的测试类是:

@RunWith(SpringRunner.class)
@ActiveProfiles("itest")
@ContextConfiguration(classes={MariadbConfiguration.class, VerticaConfiguration.class})
@AutoConfigureTestDatabase(replace=AutoConfigureTestDatabase.Replace.NONE)
@DataJpaTest
public class ApplicationIT {

    @Autowired
    DataSource mariadb;

    @Qualifier("verticaDataSource")
    @Autowired
    DataSource vertica;
    
    @Test
    public void testApplicationLoaded() {}

}

因此,如果我们MVN验证该测试没有运行

[INFO] --- maven-failsafe-plugin:2.22.2:integration-test (default) @ our-application ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

,但是如果我们将@contextConfiguntion更改为不包括Vertica

@ContextConfiguration(classes={MariadbConfiguration.class})

,则测试运行(并且会失败(并且失败) bean initiasation错误是预期的)


[INFO] --- maven-failsafe-plugin:2.22.2:integration-test (default) @ our-application ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
...
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.81 s <<< FAILURE! - in com.company.application.test.ApplicationIT
[ERROR] testApplicationLoaded  Time elapsed: 0.001 s  <<< ERROR!
org.springframework.beans.factory.UnsatisfiedDependencyException: ...
...
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

,如果我们将@contextConfigoution更改为不包括MariaDB

@ContextConfiguration(classes={VerticaConfiguration.class})

,则没有运行测试。

只是要清楚,我不是在询问错误。

问题是为什么包括verticaconfiguration.class意味着没有运行测试?似乎很奇怪。应用程序本身很好,这只是一个测试问题。

版本:

  • Java:8
  • Spring Boot:2.2.6
  • 春季框架:5.2.5
  • Junit:4.12

我们问题的一部分是我们继承了该项目。因此,我们在不正确地理解它们如何悬挂在一起的情况下使用弹簧。

I have been struggling for a couple of days with spring testing.
We are using maven-failsafe-plugin and using docker containers for our two specific databases.

But when we include the non-primary datasource configuration, no tests are run.

So config is (docker-maven-plugin sets the ip/host system properties for us):

metadata:
  datasource:
    url: jdbc:mariadb://${docker.mariadb.ip}:${docker.mariadb.port}/METADATA
    password: m_user
    username: m_pswd

reporting:
  datasource:
    jdbcUrl: jdbc:vertica://${docker.vertica.ip}:${docker.vertica.port}/REPORTING?allowMultiQueries=true
    username: v_user
    password: v_pswd

We have two database configuration classes.

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactory",
        basePackages = {"com.company.application"}
)
public class MariadbConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "metadata.datasource")
    public DataSourceProperties reportingDataSourceProperties() {
        return new DataSourceProperties();
    }


    @Primary
    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "metadata.datasource")
    public HikariDataSource dataSource(DataSourceProperties mariadbDataSourceProperties) {
        return (HikariDataSource) mariadbDataSourceProperties.initializeDataSourceBuilder()
                .type(HikariDataSource.class).build();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
        ...
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory")
            EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

    @Primary
    @Bean(name = "transactionTemplate")
    public TransactionTemplate transactionTemplate(@Qualifier("transactionManager")
            PlatformTransactionManager transactionManager) {
        return new TransactionTemplate(transactionManager);
    }
}

And

@Configuration
public class VerticaConfiguration {

    @Bean("verticaDataSourceProperties")
    @ConfigurationProperties(prefix = "reporting.datasource")
    public DataSourceProperties verticaDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "verticaDataSource")
    @ConfigurationProperties(prefix = "reporting.datasource")
    public HikariDataSource verticaDataSource() {
        return (HikariDataSource) verticaDataSourceProperties().initializeDataSourceBuilder()
                .type(HikariDataSource.class).build();
    }

    @Bean(name = "verticaJdbcTemplate")
    public JdbcTemplate verticaJdbcTemplate() {
        return new JdbcTemplate(verticaDataSource());
    }
}

So our test class is:

@RunWith(SpringRunner.class)
@ActiveProfiles("itest")
@ContextConfiguration(classes={MariadbConfiguration.class, VerticaConfiguration.class})
@AutoConfigureTestDatabase(replace=AutoConfigureTestDatabase.Replace.NONE)
@DataJpaTest
public class ApplicationIT {

    @Autowired
    DataSource mariadb;

    @Qualifier("verticaDataSource")
    @Autowired
    DataSource vertica;
    
    @Test
    public void testApplicationLoaded() {}

}

So if we mvn verify this no tests are run

[INFO] --- maven-failsafe-plugin:2.22.2:integration-test (default) @ our-application ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

BUT if we change the @ContextConfigutation to not include vertica

@ContextConfiguration(classes={MariadbConfiguration.class})

Then the test runs (and fails with bean initiasation error as expected)


[INFO] --- maven-failsafe-plugin:2.22.2:integration-test (default) @ our-application ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
...
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.81 s <<< FAILURE! - in com.company.application.test.ApplicationIT
[ERROR] testApplicationLoaded  Time elapsed: 0.001 s  <<< ERROR!
org.springframework.beans.factory.UnsatisfiedDependencyException: ...
...
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

If we change the @ContextConfigutation to not include mariadb

@ContextConfiguration(classes={VerticaConfiguration.class})

Then no tests are run.

Just to be clear, I am not asking about the error.

The question is why does including the VerticaConfiguration.class mean no tests are run? Seems very weird. Application itself is fine, this is just a testing issue.

Versions:

  • java: 8
  • spring boot: 2.2.6
  • spring framework: 5.2.5
  • junit: 4.12

Part of our problem is we kind of inherited this project. So we use spring without properly understanding how it all hangs together.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文