简体   繁体   English

Spring 多个数据源的引导测试配置

[英]Spring Boot test configuration for multiple datasources

So I am trying to introduce some unit tests for my Spring Boot application and I am having a hard time trying to setup the config for the test environment.因此,我正在尝试为我的 Spring 引导应用程序引入一些单元测试,但我很难尝试为测试环境设置配置。 My application is configured to connect to two different Postgres databases as follows:我的应用程序配置为连接到两个不同的 Postgres 数据库,如下所示:

application.properties应用程序.properties

spring.db1-datasource.jdbc-url= jdbc:postgresql://localhost:5432/db1
spring.db1-datasource.username= admin
spring.db1-datasource.password= admin
spring.db1-datasource.driverClassName= org.postgresql.Driver

spring.db2-datasource.jdbc-url= jdbc:postgresql://localhost:5432/db2
spring.db2-datasource.username= admin
spring.db2-datasource.password= admin
spring.db2-datasource.driverClassName= org.postgresql.Driver

FirstDbConfig.java FirstDbConfig.java

@Configuration
@PropertySource({"classpath:application.properties"})
@EnableJpaRepositories(
        basePackages = "org.myapp.database.db1.repository",
        entityManagerFactoryRef = "firstEntityManager",
        transactionManagerRef = "firstTransactionManager")
public class FirstDbConfig {
    @Autowired
    private Environment env;

    public FirstDbConfig() {
        super();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean firstEntityManager() {
        final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(firstDataSource());
        em.setPackagesToScan("org.myapp.database.db1");

        final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        final HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
        em.setJpaPropertyMap(properties);

        return em;
    }

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

    @Bean
    public PlatformTransactionManager firstTransactionManager() {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(firstEntityManager().getObject());
        return transactionManager;
    }
}

SecondDbConfig.java SecondDbConfig.java

Not including it because it's quite similar.不包括它,因为它非常相似。 Targets org.myapp.database.db2.repository目标org.myapp.database.db2.repository

For my basic test, I am trying to test a simple service that injects one of these configured repositories.对于我的基本测试,我正在尝试测试一个注入这些已配置存储库之一的简单服务。 This looks like that:这看起来像这样:

MyServiceTest.java我的服务测试.java

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyService.class)
@ContextConfiguration(
        classes = {FirstDbConfig.class, SecondDbConfig.class },
        loader= AnnotationConfigContextLoader.class
)
public class MyServiceTest {

    @InjectMocks
    private MyService myService;

    @Mock
    private FirstDbRepository dbRepository;

    @Test
    public void test() {
        ...
        // call to myService.method()
    }
}

Test fails with:测试失败:

java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set at ...

It obviously has to do with hibernate.dialect not being set but no idea why that is the case.它显然与未设置hibernate.dialect ,但不知道为什么会这样。 Any ideas?有任何想法吗?

Yes, we need to include this hibernate dialect, try including these 2 lines in your application.properties file是的,我们需要包含这个 hibernate 方言,尝试在 application.properties 文件中包含这两行

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = update

需要在 application.properties 文件中包含这个 spring.jpa.database=default

You need to set the environment property hibernate.dialect since you're trying to get its value in your code.您需要设置环境属性hibernate.dialect ,因为您正试图在代码中获取它的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM