我试图使用springboot2.0.1测试我的存储库层,但是当我运行我的测试类时,spring试图示例化一个配置类,而不是从测试包中。
测试代码如下:
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。
1条答案
按热度按时间vatpfxk51#
在你的
LogRepositoryTest
测试类您应该指出应该考虑的备用测试配置类,在您的情况下,我认为应该是TestConfig
.从spring boot文档:
如果您熟悉spring测试框架,您可能会习惯使用
@ContextConfiguration(classes=…)
以指定哪个Spring@Configuration
装载。或者,您可能经常使用嵌套@Configuration
测试中的类。所以注解
LogRepositoryTest
与@ContextConfiguration(classes = {TestConfig.class})
```@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("local")
@ContextConfiguration(classes = {TestConfig.class})
public class LogRepositoryTest {
}
@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 {
//...
}
@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) {
// ...
}