Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use @DataJpaTest with multiple datasourse

Tags:

spring-boot

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

then i tried to add a annotation

@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.AUTO_CONFIGURED)

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

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

}
like image 868
Mikhail Avatar asked Aug 26 '16 13:08

Mikhail


People also ask

How do you handle multiple data sources in spring boot?

So, to use multiple data sources, we need to declare multiple beans with different mappings within Spring's application context. The configuration for the data sources must look like this: spring: datasource: todos: url: ... username: ...

Can we connect to multiple databases in spring boot?

Multiple Databases in Spring Boot Spring Boot can simplify the configuration above. Now we have defined the data source properties inside persistence-multiple-db-boot. properties according to the Boot autoconfiguration convention.

What is the use of @DataJpaTest annotation?

@DataJpaTest is used to test JPA repositories. It is used in combination with @RunWith(SpringRunner. class) . The annotation disables full auto-configuration and applies only configuration relevant to JPA tests.

What is @AutoConfigureTestDatabase?

Annotation Type AutoConfigureTestDatabase Annotation that can be applied to a test class to configure a test database to use instead of the application-defined or auto-configured DataSource . In the case of multiple DataSource beans, only the @Primary DataSource is considered.


2 Answers

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
like image 188
Joel Mata Avatar answered Sep 24 '22 07:09

Joel Mata


Check if you have h2 database added as dependency in test scope. If not, add and try:

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>
like image 32
user18278046 Avatar answered Sep 23 '22 07:09

user18278046