当前位置 : 主页 > 网络安全 > 测试自动化 >

弹簧重试单元测试

来源:互联网 收集:自由互联 发布时间:2021-06-19
我在maven项目中使用 spring retry( http://docs.spring.io/spring-retry/docs/1.1.2.RELEASE/apidocs/),我有以下单元测试 @RunWith(SpringJUnit4ClassRunner.class)@ContextConfigurationpublic class RetriableFileManagerTest { @Autowired
我在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)}
最后想出了问题,我不得不将@Retryable注释移动到AssetResource实现的接口,并将此接口类型自动装配到单元测试.
网友评论