Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何将@DataJpaTest与多个数据源一起使用_Spring Boot - Fatal编程技术网

Spring boot 如何将@DataJpaTest与多个数据源一起使用

Spring boot 如何将@DataJpaTest与多个数据源一起使用,spring-boot,Spring Boot,我尝试使用annotation@DataJpaTest编写集成测试。 我有两个数据源:主数据源和辅助数据源(类配置) 结果我有一个错误: expected single matching bean but found 2: primaryDataSource,secondary 然后我尝试添加一个注释 @AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.AUTO_CONFIGURED) 使用AUTO_配置

我尝试使用annotation@DataJpaTest编写集成测试。 我有两个数据源:主数据源和辅助数据源(类配置) 结果我有一个错误:

expected single matching bean but found 2: primaryDataSource,secondary
然后我尝试添加一个注释

@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.AUTO_CONFIGURED)
使用AUTO_配置时,只会替换由属性配置的数据源,而不是嵌入h2,我看到方言:hh000400:使用方言:org.hibernate.dialent.oracle10galent

如何对多个数据源使用
@DataJpaTest

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();
    }

}
找到了一个可能的解决办法

基本上,您可以手动适当地配置H2数据库,而不是让Spring自动进行配置

使用以下内容在“src/test/resources”中创建application.properties文件

# Let Spring autodetect the different SQL Dialects of each datasource
spring.jpa.database=default
# Generate the DB schema in the In-Memory H2 databases based on the JPA Entities
spring.jpa.generate-ddl=true

# H2 In-Memory Database "foo" (used in tests instead of a real PostgreSQL DB)
spring.datasource.url=jdbc:h2:mem:foo;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

# H2 In-Memory Database "bar" (used in tests instead of a real PostgreSQL DB)
bar.datasource.url=jdbc:h2:mem:bar;DB_CLOSE_ON_EXIT=FALSE
bar.datasource.username=sa
bar.datasource.password=
bar.datasource.driver-class-name=org.h2.Driver

我有一个@Primary配置类

@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);
    }
}
稍后,在我的存储库测试类中:

@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles("test")
@Import(APrimaryDBDBConfiguration.class)
最后,我的测试属性有:

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

这不是很有帮助。如何构建
数据源?使用
@SpringBootTest
并不是一个切片测试,因为它加载所有应用程序上下文spring本身将根据上述属性的命名约定为您创建数据源。如果您不喜欢@SpringBootTest,那么您可以使用@RunWith(SpringJUnit4ClassRunner.class)和其他Spring注释(例如@ContextConfiguration)来控制类中包含应用程序的哪些部分。您找到解决方案了吗?