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

Java 多个数据源的Spring引导测试配置,java,postgresql,spring-boot,spring-test,Java,Postgresql,Spring Boot,Spring Test,因此,我试图为我的Spring Boot应用程序引入一些单元测试,并且我很难为测试环境设置配置。我的应用程序配置为连接到两个不同的Postgres数据库,如下所示: 应用程序属性 spring.db1-datasource.jdbc-url= jdbc:postgresql://localhost:5432/db1 spring.db1-datasource.username= admin spring.db1-datasource.password= admin spring.db1-data

因此,我试图为我的Spring 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
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;
    }
}

@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方言,请尝试在application.properties文件中包含这两行

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

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

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

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

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

重新检查数据库的时间。它是否在本地主机上运行和侦听?它正在监听端口5432吗?模式db1和db2实际上存在于其中吗?凭证是否正确?原因是此错误意味着无法自动检测到必要的详细信息,因此要求您自己提供这些详细信息,但这没有帮助,因为无法自动检测到这些详细信息的原因有问题。@Gimby一切都已启动并运行,在开发环境中运行良好。但是测试是否在同一个平台上运行开发环境?是时候重新检查数据库了。它是否在本地主机上运行和侦听?它正在监听端口5432吗?模式db1和db2实际上存在于其中吗?凭证是否正确?原因是此错误意味着无法自动检测到必要的详细信息,因此要求您自己提供这些详细信息,但这没有帮助,因为无法自动检测到这些详细信息的原因有问题。@Gimby一切都已启动并运行,在开发环境中运行良好。但是测试是否在同一个平台上运行开发环境?