我试图使用Spring Boot 2.0.1来测试我的存储库层,但是当我运行测试类时,Spring尝试不从测试包中实例化Config类。
这是测试代码:
TestConfig.class
@Configuration
@Import(value = {TestDatabaseConfig.class})
@Profile("local")
public class TestConfig {
}
TestDatabaseConfig.class
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "logEntityManagerFactory",
transactionManagerRef = "logTransactionManager",
basePackages = { "it.xxx.yyy.repository.log" })
@EntityScan(basePackages = {"it.xxx.yyy.model.log", "it.xxx.yyy.common"})
@Profile("local")
public class TestDatabaseConfig {
@Bean("logDataSourceProperties")
public DataSourceProperties logDataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "logDataSource")
public DataSource dataSource(@Qualifier("logDataSourceProperties") DataSourceProperties properties) {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:schema.sql")
.build();
}
@Bean(name = "logEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean logEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("logDataSource") DataSource logDataSource) {
return builder.dataSource(logDataSource)
.packages("it.xxx.model.log")
.persistenceUnit("log")
.build();
}
@Bean(name = "logTransactionManager")
public PlatformTransactionManager logTransactionManager(@Qualifier("logEntityManagerFactory")EntityManagerFactory logEntityManagerFactory) {
return new JpaTransactionManager(logEntityManagerFactory);
}
}
运行此类时
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("local")
public class LogRepositoryTest {
@Autowired
private ResultLogRepository resultLogRepository;
@Test
public void init(){
}
}
它说:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'kafkaProducer': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'kafka.topic.operation' in value "${kafka.topic.operation}"
[...]
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'kafka.topic.operation' in value "${kafka.topic.operation}"
但是我不明白为什么它会从我的主程序包(上面带有@Configuration注释)中调出我的KafkaProducer.class。
答案 0 :(得分:1)
在您的LogRepositoryTest
测试类中,您应该指出应该考虑的替代测试配置类,在您的情况下,我认为应该是TestConfig
。
从Spring Boot documentation:
如果您熟悉Spring Test Framework,则可能习惯于使用
@ContextConfiguration(classes=…)
来指定要加载哪个Spring@Configuration
。另外,您可能经常在测试中使用嵌套的@Configuration
类。
因此用LogRepositoryTest
注释@ContextConfiguration(classes = {TestConfig.class})
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("local")
@ContextConfiguration(classes = {TestConfig.class})
public class LogRepositoryTest {
@Autowired
private ResultLogRepository resultLogRepository;
@Test
public void init(){
}
}
更新
还使用以下注释您的配置类:
@EnableAutoConfiguration
类似:
@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "logEntityManagerFactory",
transactionManagerRef = "logTransactionManager",
basePackages = { "it.xxx.yyy.repository.log" })
@EntityScan(basePackages = {"it.xxx.yyy.model.log", "it.xxx.yyy.common"})
@Profile("local")
public class TestDatabaseConfig {
//...
}
更新2
对于错误:
由以下原因引起:org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有类型为'org.springframework.boot.autoconfigure.jdbc.DataSourceProperties'的合格Bean:预期为单个匹配的Bean,但发现2:logDataSourceProperties,spring.datasource- org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
完全删除该方法:
@Bean("logDataSourceProperties")
public DataSourceProperties logDataSourceProperties() {
return new DataSourceProperties();
}
并更改您的:
@Bean(name = "logDataSource")
public DataSource dataSource(@Qualifier("logDataSourceProperties") DataSourceProperties properties) {
// ...
}
至:
@Bean(name = "logDataSource")
public DataSource dataSource(DataSourceProperties properties) {
// ...
}