Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring retry unit test

Tags:

spring

junit4

I am using spring retry (http://docs.spring.io/spring-retry/docs/1.1.2.RELEASE/apidocs/) in a maven project and I have the following unit test

@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;
        }
    }
}

However when I try to run the test it fails with

    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)}
like image 464
Jonathan Avatar asked Aug 11 '16 10:08

Jonathan


People also ask

How do I test retry logic Spring boot?

Implementing the Retry Logic. First, you need to enable Spring Retry. You can achieve this by adding the @EnableRetry annotation to your @SpringBootApplication or @Configuration class. You can now use @Retryable to annotate any method to be a candidate or retry and @Recover to specify fallback methods.

What is spring retry used for?

Spring Retry provides an ability to automatically re-invoke a failed operation. This is helpful where the errors may be transient (like a momentary network glitch). In this tutorial, we'll see the various ways to use Spring Retry: annotations, RetryTemplate, and callbacks.

Is Spring retry a circuit breaker?

Spring Retry provides a circuit breaker implementation via a combination of it's CircuitBreakerRetryPolicy and a stateful retry. All circuit breakers created using Spring Retry will be created using the CircuitBreakerRetryPolicy and a DefaultRetryState .


2 Answers

Finally figured out the issue, I had to move the @Retryable annotation to the interface that AssetResource implements and autowire this interface type to the unit test.

like image 160
Jonathan Avatar answered Nov 11 '22 18:11

Jonathan


Error message says

No qualifying bean of type [ctp.cms.actions.handlers.repository.resources.AssetResource] found for dependency

How is ctp.cms.actions.handlers.repository.resources.AssetResource declared?

Do you have @Component (@Service or similar annotation on it?) Is this package enabled for @ComponentScan ?

like image 33
Bartosz Bilicki Avatar answered Nov 11 '22 20:11

Bartosz Bilicki