简体   繁体   中英

Spring boot two data sources, unit tests stopped working

I have switched my application to use two data sources, the code runs fine and both are picked up, however my unit tests have started to fail, code is below, help much appreciated.

Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:180)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:120)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 71 more

application.properties (in main and in test folders):

datasource.primary.url = <url>
datasource.primary.username = <user>
datasource.primary.password = <password>

datasource.secondary.url = <url>
datasource.secondary.username = <user>
datasource.secondary.password = <pass>

Main program:

@EnableAutoConfiguration
@Configuration
@EntityScan({"com.example.domain","com.example.common.domain"})
@PropertySource(value = "classpath:application.properties")
@EnableScheduling
    public class MyApplication {

public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);
}

  }

Primary Data source config:

@Configuration
@EnableJpaRepositories(basePackages = "com.example.repository", 
entityManagerFactoryRef = "primaryEntityManagerFactory", 
transactionManagerRef = "primaryTransactionManager")
@EnableTransactionManagement
public class PrimaryConfiguration {

@Bean
@ConfigurationProperties(prefix = "datasource.primary")
@Primary
public DataSource primaryDataSource()
{
    return DataSourceBuilder.create().build();
}

@Bean
@Primary
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(final EntityManagerFactoryBuilder builder)
{
    return builder
            .dataSource(primaryDataSource())
            .packages("uk.gov.dwp.pss.roc.domain")
            .persistenceUnit("primaryPersistenceUnit")
            .build();
}

@Bean
@Primary
public JpaTransactionManager primaryTransactionManager(@Qualifier("primaryEntityManagerFactory") final EntityManagerFactory factory)
{
    return new JpaTransactionManager(factory);
}
}

Secondary config class:

@Configuration
@EnableJpaRepositories(basePackages = "com.example.common.repository", 
entityManagerFactoryRef = "secondaryEntityManagerFactory", 
transactionManagerRef = "secondaryTransactionManager")
@EnableTransactionManagement
public class SecondaryConfiguration {

@Bean
@ConfigurationProperties(prefix = "datasource.secondary")
public DataSource secondaryDataSource()
{
    return DataSourceBuilder.create().build();
}

@Bean
public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(final EntityManagerFactoryBuilder builder)
{
    return builder
            .dataSource(secondaryDataSource())
            .packages("uk.gov.dwp.pss.commons.domain.security")
            .persistenceUnit("secondaryPersistenceUnit")
            .build();
}

@Bean
public JpaTransactionManager secondaryTransactionManager(@Qualifier("secondaryEntityManagerFactory") final EntityManagerFactory factory)
{
    return new JpaTransactionManager(factory);
}

}

Repository class:

public interface MyRepository extends JpaRepository<MyObject, String>,JpaSpecificationExecutor<MyObject> {
}

Unit test class use annotation:

@SpringApplicationConfiguration(classes = MyApplication.class)

在运行测试用例时要在自动配置设置的类路径中找不到数据库驱动程序。

As @ndrone stated you need to set your database driver. It is unclear if you are attempting to use AutoConfigureTestDatabase. If so you should reference AutoConfigureTestDatabase from the Spring Boot docs.

If this is the case you could specify the Embedded database to use by using the following in your test (configuration):

@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)

Otherwise, you could modify your application.properties in your test folder to specify the connection type such as:

datasource.primary.driver-class-name=org.h2.Driver
datasource.secondary.driver-class-name=org.h2.Driver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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