@Repository未在@DataJpaTest中自动装配,并且在一个配置文件中导入了多个@Configuration文件

时间:2017-11-23 20:38:13

标签: spring spring-data-jpa spring-test

我有三个@Configuration文件,如下所示:

MessageGatewayConfiguration.class

std_logic

SpringConfiguration.class

player <= not player

SmsFireboltConfiguration.class

 @Configuration
 @EnableAutoConfiguration
 @EnableJpaRepositories(basePackages = {
            "xyz.idtlabs.smsgateway.*"
})
@EntityScan(basePackageClasses = {
        AbstractPersistableCustom.class,
        SMSBridge.class,
        SMSBridgeConfig.class,
        SMSMessage.class,
        Tenant.class
})
@ComponentScan(basePackages = {
        "xyz.idtlabs.smsgateway.*"
})
public class MessageGatewayConfiguration {  
    public MessageGatewayConfiguration() {
        super();
    } 
    @Bean
    public SimpleApplicationEventMulticaster applicationEventMulticaster() {
        final SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
        multicaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
        return multicaster;
    } 
    @Bean
    public HandlerExceptionResolver sentryExceptionResolver() {
        return new io.sentry.spring.SentryExceptionResolver();
    }     
} 

主要申请文件如下:

@Configuration
@EnableMetrics
public class SpringConfiguringClass extends MetricsConfigurerAdapter {

    @Override
    public void configureReporters(MetricRegistry metricRegistry) {
        // registerReporter allows the MetricsConfigurerAdapter to
        // shut down the reporter when the Spring context is closed
        registerReporter(ConsoleReporter
            .forRegistry(metricRegistry)
            .build())
            .start(1, TimeUnit.MINUTES);
    }
} 

但是,我的Spring @DataJpaTest集成测试没有自动装配TenantRepository JPA存储库,“没有类型'xyz.idtlabs.smsgateway.tenants.repository.TenantRepository'的限定bean可用:预计至少有1个bean符合条件autowire候选人“错误。 其代码如下:

@Configuration
@Import({ MessageGatewayConfiguration.class, SpringConfiguringClass.class })
public class SmsFireboltConfiguration { 
} 

为什么会这样?我已经尝试了许多设置@DataJpaTest文件的组合,但无济于事。

1 个答案:

答案 0 :(得分:0)

您将非配置类指定为@ContextConfiguration

您需要的是:

@ContextConfiguration(classes = MessageGatewayConfiguration.class)

如果您想要包含所有Spring配置,请执行以下操作:

@ContextConfiguration(classes = SmsFireboltConfiguration.class)