mardi 26 septembre 2017

No qualifying bean of type found for dependency in test class

I have a spring boot application that has a test class as following:

@ContextConfiguration(value = {"classpath:integration/flex-allowcation-assignment-test.xml"})
public class FlexAllowanceAssignmentServiceIntegrationTest extends AbstractSecurityAwareIntegrationTest {

    @Autowired
    private FlexAllowanceAssignmentTestUtils assignmentEndpoint;

    @Test
    public void shouldReturnFailureResponse() {
        // Given
        String message = randomString();
        UserStub user = userBuilder().build();

        // When
        FlexAllowanceServiceResponse response = assignmentEndpoint.assign(message, user);

        // Then
        assertThat(response).isEqualTo(FAILURE);
    }
}

, The FlexAllowanceAssignmentTestUtils class is:

public class FlexAllowanceAssignmentTestUtils {

    private final FlexAllowanceAssignmentGateway flexAllowanceAssignmentGateway;
    private final JacksonWrapper jacksonWrapper;

    @Autowired
    public FlexAllowanceAssignmentTestUtils(FlexAllowanceAssignmentGateway flexAllowanceAssignmentGateway, JacksonWrapper jacksonWrapper) {
        this.flexAllowanceAssignmentGateway = flexAllowanceAssignmentGateway;
        this.jacksonWrapper = jacksonWrapper;
    }

    public FlexAllowanceServiceResponse assign(String message, UserStub user) {
        Message<String> requestMessage = MessageBuilder
                .withPayload(message)
                .build();
        Message<?> response = flexAllowanceAssignmentGateway.assign(enhanceWithSecurityContext(requestMessage, user));
        String payload = (String) response.getPayload();
        return jacksonWrapper.fromJson(payload, FlexAllowanceServiceResponse.class);
    }
}

, and the flex-allowcation-assignment-test.xml file as below:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
       xmlns:xsi="http://ift.tt/ra1lAU"
       xmlns:int="http://ift.tt/1eF5VEE"
       xmlns:int-jms="http://ift.tt/TAOyvj"
       xsi:schemaLocation="http://ift.tt/GArMu6 http://ift.tt/1jdM0fG
       http://ift.tt/1eF5VEE http://ift.tt/1eF5VEQ
       http://ift.tt/TAOyvj http://ift.tt/1qGwG2k">

    <int-jms:outbound-gateway
            request-destination-name="UPL.EMPLOYEE.FLEX-ALLOWANCE.ASSIGNMENT.1.REQ"
            request-channel="flexAllowanceAssignmentRequestChannel"
            receive-timeout="60000"/>

    <int:channel id="flexAllowanceAssignmentRequestChannel"/>

    <int:gateway id="flexAllowanceAssignmentGateway"
                 service-interface="com.orbitbenefits.benefitsselection.server.employee.flexallowance.FlexAllowanceAssignmentGateway"
                 default-request-channel="flexAllowanceAssignmentRequestChannel"/>

    <bean class="com.orbitbenefits.benefitsselection.server.employee.flexallowance.FlexAllowanceAssignmentTestUtils"/>

</beans>

When trying to run the FlexAllowanceAssignmentServiceIntegrationTest, I get the following exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.orbitbenefits.benefitsselection.server.employee.flexallowance.FlexAllowanceAssignmentServiceIntegrationTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.orbitbenefits.benefitsselection.server.employee.flexallowance.FlexAllowanceAssignmentTestUtils com.orbitbenefits.benefitsselection.server.employee.flexallowance.FlexAllowanceAssignmentServiceIntegrationTest.assignmentEndpoint; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.orbitbenefits.benefitsselection.server.employee.flexallowance.FlexAllowanceAssignmentTestUtils] 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=assignmentEndpoint)}
...
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.orbitbenefits.benefitsselection.server.employee.flexallowance.FlexAllowanceAssignmentTestUtils com.orbitbenefits.benefitsselection.server.employee.flexallowance.FlexAllowanceAssignmentServiceIntegrationTest.assignmentEndpoint; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.orbitbenefits.benefitsselection.server.employee.flexallowance.FlexAllowanceAssignmentTestUtils] 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=assignmentEndpoint)}
    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 [com.orbitbenefits.benefitsselection.server.employee.flexallowance.FlexAllowanceAssignmentTestUtils] 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=assignmentEndpoint)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 27 more

How come it can not see the bean declared in the config class- or shall I better declare that elsewhere? Or could that be failing because it doesn't have dependencies (FlexAllowanceAssignmentGateway and JacksonWrapper)?

Aucun commentaire:

Enregistrer un commentaire