我在maven项目中使用spring retry(http://docs.spring.io/spring-retry/docs/1.1.2.RELEASE/apidocs/)并且我有以下单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class RetriableFileManagerTest {
@Autowired
@Qualifier("asset")
private AssetResource assetResource;
@Test
public void testRetry() throws URISyntaxException {
byte[] image = this.assetResource.fetchResource("name", "path");
verify(assetResource, times(3)).fetchResource("name", "path");
Assert.assertEquals("should be equal", "image", new String(image));
}
@Configuration
@EnableRetry
public static class SpringConfig {
@Bean(name = "asset")
public AssetResource assetResource() throws Exception {
AssetResource remoteService = mock(AssetResource.class);
when(remoteService.fetchResource(anyString(), anyString()))
.thenThrow(new RuntimeException("Remote Exception 1"))
.thenThrow(new RuntimeException("Remote Exception 2"))
.thenReturn("Completed".getBytes());
return remoteService;
}
}
}
然而,当我尝试运行测试时,它失败并带有
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ctp.cms.actions.handlers.repository.resources.AssetResource ctp.cms.actions.handlers.filemanager.RetriableFileManagerTest.assetResource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ctp.cms.actions.handlers.repository.resources.AssetResource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=asset)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 25 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ctp.cms.actions.handlers.repository.resources.AssetResource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=asset)}
答案 0 :(得分:3)
最后找出了问题,我不得不将@Retryable
注释移动到AssetResource
实现的接口,并将此接口类型自动装配到单元测试中。
答案 1 :(得分:2)
错误消息说
没有类型的限定bean 找到[ctp.cms.actions.handlers.repository.resources.AssetResource] 依赖
ctp.cms.actions.handlers.repository.resources.AssetResource
如何宣布?
您是否有@Component
(@Service
或类似的注释?)是否为@ComponentScan
启用了该套餐?