我有一个Spring启动应用程序,其测试类如下:
@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);
}
}
, FlexAllowanceAssignmentTestUtils 类是:
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);
}
}
和 flex-allowcation-assignment-test.xml 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
<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>
尝试运行 FlexAllowanceAssignmentServiceIntegrationTest 时,出现以下异常:
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
为什么它看不到在config类中声明的bean-或者我最好在其他地方声明?或者这可能会失败,因为它没有依赖关系(FlexAllowanceAssignmentGateway和JacksonWrapper)?