通过@SpringJUnitConfig与@SpringBootTest执行测试时的区别

问题描述 投票:0回答:1

在我们的测试代码库中,我们试图尽量减少

@SpringBootTest
注释的使用,以支持更有针对性的
@SpringJUnitConfig

在第一次工作测试中更换它:

@SpringJUnitConfig
//@SpringBootTest
class DataMarshallerServiceTest {

    @Autowired
    private DataMarshallerService cut;
    

我们开始看到以下堆栈:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.abc.globalpayments.feeds.downstream.dailycashreport.generate.DataMarshallerService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1801)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1357)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659)
    ... 75 common frames omitted

同样,在构建过程中:

mvn clean package -P mvnProfile

[ERROR] Errors:                                                                                                                                                        
[ERROR]   DataMarshallerServiceTest.testSuccessfulFileGenerationViaBatches � UnsatisfiedDependency Error creating bean with name 'com.abc.globalpayments.feeds.downstre
am.dailycashreport.generate.DataMarshallerServiceTest': Unsatisfied dependency expressed through field 'cut'; nested exception is org.springframework.beans.factory.NoS
uchBeanDefinitionException: No qualifying bean of type 'com.abc.globalpayments.feeds.downstream.dailycashreport.generate.DataMarshallerService' available: expected at 
least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}                   
[ERROR]   DataMarshallerServiceTest.testTrailerAggregationsProduction � UnsatisfiedDependency Error creating bean with name 'com.abc.globalpayments.feeds.downstream.da
ilycashreport.generate.DataMarshallerServiceTest': Unsatisfied dependency expressed through field 'cut'; nested exception is org.springframework.beans.factory.NoSuchBe
anDefinitionException: No qualifying bean of type 'com.abc.globalpayments.feeds.downstream.dailycashreport.generate.DataMarshallerService' available: expected at least
 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}                        
[INFO]                                                                                                                                                                 

参与这些测试的,有作为

@Bean
类的一部分创建的
@Configuration
,以及装饰有
@Component
@Service
的类,可通过类路径扫描进行解析。

例如上面被测试的类:

@Slf4j
@Service
public class DataMarshallerService {//}

src/main/resources
包含
application-dev.yml
属性文件,而
application.yml
中有相应的
src/test/resources

@SpringJUnitConfig
不进行正常的类路径扫描吗?此设置中可能缺少什么或配置错误?

提前谢谢您。

spring-boot testing dependency-injection spring-test
1个回答
0
投票

我的理解是您需要指出要使用的配置。您可以使用 Config.class 或 spring 配置文件。

配置类示例:

@SpringJUnitConfig(TestConfig.class) 

配置文件示例:

@SpringJUnitConfig(locations = "/test-config.xml")
© www.soinside.com 2019 - 2024. All rights reserved.