2017-02-17 7 views
1

2つのデータソースを使用するようにアプリケーションを切り替えると、コードは正常に実行され、両方が選択されますが、単体テストが失敗し、 。 (メインとのテストフォルダ内)スプリングブート2データソース、単体テストが動作しなくなった

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

@ndroneは、データベースドライバを設定する必要があると述べています。 AutoConfigureTestDatabaseを使用しようとしているかどうかは不明です。その場合は、SpringブートのドキュメントからAutoConfigureTestDatabaseを参照する必要があります。

これはあなたのテスト(コンフィギュレーション)で次のように使用して、使用する組み込みデータベースを指定することができそうである場合:それ以外の場合は

@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2) 

、あなたが指定するテストフォルダにapplication.propertiesを修正することができます

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

アプリケーションのプロパティファイルにデータベースドライバを追加しましたが、まったく同じエラーが表示されます。私はAutoConfigureTestDatabaseを使用しようとしていません – helpme7766

関連する問題