我的应用程序中有两个数据源,当连接到实时环境中的数据库时,它们可以按预期工作。但是,在为此应用程序编写单元测试时,我的bean定义遇到了问题。
这是我的主要数据源配置:
@Configuration
@EnableJpaRepositories(
basePackages = "foo.bar.repository.primary",
entityManagerFactoryRef = "primaryEntityManager",
transactionManagerRef = "primaryTransactionManager")
public class PrimaryDataSourceConfig {
@Bean
@Primary
@ConfigurationProperties("primary.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean primaryEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(primaryDataSource());
em.setPackagesToScan("foo.bar.domain.entity");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
Map<String, String> properties = new HashMap<>();
properties.put("hibernate.implicit_naming_strategy",
"org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy");
properties.put("hibernate.physical_naming_strategy",
"org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
em.setJpaPropertyMap(properties);
return em;
}
@Bean
@Primary
public PlatformTransactionManager primaryTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(primaryEntityManager().getObject());
return transactionManager;
}
}
我有一个测试,我想要做的就是旋转整个应用程序上下文,就像这样。
@AutoConfigureTestDatabase
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ErpApplicationTest {
@Test
public void test() {
// Application started
}
}
但是,当我运行此测试时,出现以下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'primaryEntityManager' defined in class path resource [foo/bar/primaryDataSourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'primaryEntityManager' threw exception; nested exception is java.lang.IllegalArgumentException: No visible constructors in class org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean
是什么导致此错误,我该怎么办才能纠正它? Flyway能够连接到注释生成并运行其迁移的嵌入式H2实例,但是主DataSource及其EntityManager会在创建时失败。
答案 0 :(得分:0)
我通过以下操作解决了该问题:
首先,我将AutoConfigureTestDatabase注释更改为不替换任何数据源:
@AutoConfigureTestDatabase(
replace = AutoConfigureTestDatabase.Replace.NONE)
我现在遇到错误,我将对数据库超时。由于AutoConfig默认为内存h2,因此我认为以前的数据库连接设置不正确。我将application.properties文件更改为包含以下内容:
primary.datasource.driver-class-name=org.h2.Driver
primary.datasource.jdbc-url=jdbc:h2:~;MODE=MYSQL
primary.datasource.username=
primary.datasource.password=
secondary.datasource.driver-class-name=org.h2.Driver
secondary.datasource.jdbc-url=jdbc:h2:~;MODE=MYSQL
secondary.datasource.username=
secondary.datasource.password=
大概是因为自动配置无法正确替换我的自定义数据源的entityManager而发生此错误。通过确保未替换DataSource并确保连接URL正确,测试得以按预期进行。