spring boot测试错误的配置类

cvxl0en2  于 2021-06-08  发布在  Kafka
关注(0)|答案(1)|浏览(453)

我试图使用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。

vatpfxk5

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 {

@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) {
// ...
}

相关问题