本文介绍了如何将@DataJpaTest 与多个数据源一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用注释 @DataJpaTest 编写集成测试.我有两个数据源:主要和次要(类配置)结果我有一个错误:

I tried to write integration test using annotation @DataJpaTest .I have two datasource: Primary and secondary (class config)in result i have an error:

expected single matching bean but found 2: primaryDataSource,secondary

然后我尝试添加注释

@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.AUTO_CONFIGURED)

和使用 AUTO_CONFIGURED 仅由属性配置的数据源将被替换,而是嵌入 h2 我看到了 Dialect : HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect

and With AUTO_CONFIGURED only DataSources configured by properties will be replaced but instead embedded h2 i saw Dialect : HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect

如何在多个数据源中使用 @DataJpaTest ?

how using @DataJpaTest with multiple datasources ?

public class DataSourcesConfig {


    @Bean
    @Primary
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondary")
    @ConfigurationProperties(prefix="datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }

}

推荐答案

我有一个@Primary 配置类

I have a @Primary configuration class

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = {"com.something"}
)
public class APrimaryDBDBConfiguration {

    @Primary
    @Bean
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }

    @Primary
    @Bean(name = "dataSource")
    @ConfigurationProperties("spring.datasource.hikari")
    public HikariDataSource dataSource() {
        return dataSourceProperties()
                .initializeDataSourceBuilder()
                .type(HikariDataSource.class).build();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    @Profile("!test")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("dataSource") DataSource dataSource) {
        return builder
                .dataSource(dataSource)
                .packages("com")
                .persistenceUnit("some_persistence_unit")
                .build();
    }

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

稍后,在我的存储库测试类中:

Later, in my repository test classes:

@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles("test")
@Import(APrimaryDBDBConfiguration.class)

最后,我的测试属性有:

Finally, my test properties have:

spring.jpa.hibernate.ddl-auto=create-drop

这篇关于如何将@DataJpaTest 与多个数据源一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 14:02