我已经将我的应用程序切换为使用两个数据源,代码运行良好并且两者都被拾取,但是我的单元测试已经开始失败,代码在下面,非常感谢。
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(在主文件夹和测试文件夹中):
datasource.primary.url = <url>
datasource.primary.username = <user>
datasource.primary.password = <password>
datasource.secondary.url = <url>
datasource.secondary.username = <user>
datasource.secondary.password = <pass>
主程序:
@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);
}
}
主数据源配置:
@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);
}
}
辅助配置类:
@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);
}
}
存储库类:
public interface MyRepository extends JpaRepository<MyObject, String>,JpaSpecificationExecutor<MyObject> {
}
单元测试类使用注释:
@SpringApplicationConfiguration(classes = MyApplication.class)
答案 0 :(得分:0)
在运行测试用例时要在自动配置设置的类路径中找不到数据库驱动程序。
答案 1 :(得分:0)
正如@ndrone所说,你需要设置你的数据库驱动程序。目前还不清楚您是否尝试使用AutoConfigureTestDatabase。如果是这样,您应该参考Spring Boot文档中的AutoConfigureTestDatabase。
如果是这种情况,您可以在测试(配置)中使用以下内容指定要使用的嵌入式数据库:
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
否则,您可以修改测试文件夹中的application.properties以指定连接类型,例如:
datasource.primary.driver-class-name=org.h2.Driver
datasource.secondary.driver-class-name=org.h2.Driver