postgresql 多个数据源的Sping Boot 测试配置

hgb9j2n6  于 2022-11-23  发布在  PostgreSQL
关注(0)|答案(3)|浏览(152)

因此,我尝试为我的Sping Boot 应用程序引入一些单元测试,但我很难为测试环境设置配置。我的应用程序被配置为连接到两个不同的Postgres数据库,如下所示:

应用程序.属性

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

第一个数据库配置.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;
    }
}

第二个数据库配置.java

不包括它,因为它非常相似。目标org.myapp.database.db2.repository
对于我的基本测试,我尝试测试一个简单的服务,它注入这些配置的仓库之一。

我的服务测试.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()
    }
}

测试失败:

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

很明显,这与hibernate.dialect没有被设置有关,但不知道为什么会这样,有什么想法吗?

bvjveswy

bvjveswy1#

是的,我们需要包含此Hibernate方言,请尝试在www.example.com文件中包含这两行application.properties

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

t1qtbnec2#

需要在application.properties文件spring.jpa.database =default中包含此内容

eivnm1vs

eivnm1vs3#

您需要设置环境属性hibernate.dialect,因为您试图在代码中获取它的值。

相关问题