我们有一个以下形式的bean构造函数:
<bean id="myMapper" class="com.mapper.internet.GenericMapper">
<constructor-arg ref="service1"/>
<constructor-arg ref="service2"/>
<constructor-arg ref="service3"/>
<constructor-arg ref="anotherMapper"/>
<constructor-arg type="boolean" value="false"/>
<constructor-arg> <value type="com.mapper.internet.AbstractGenericMapper.myType">MY_TYPE_VALUE</value>
</constructor-arg>
</bean>
这适用于生产,但不是在我们使用JUnit运行自动化测试时。我们得到的错误是:
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'yetAnotherService' defined in class path resource [testBusinessApplicationContext.xml]:
Cannot resolve reference to bean 'myLookup' while setting constructor argument;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'internetQuoteMapperLookup' defined in class path resource [testBusinessApplicationContext.xml]:
Cannot resolve reference to bean 'apiTimeWarnerCableEOFSymmetricQuoteMapper'
while setting bean property 'quoteMapperMap' with key [TypedStringValue: value [API_EOF_SYMMETRIC:TIME WARNER CABLE], target type [null]];
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'apiTimeWarnerCableEOFSymmetricQuoteMapper' defined in class path resource [testBusinessApplicationContext.xml]:
Unsatisfied dependency expressed through constructor argument with index 5 of type [java.lang.String]:
Could not convert constructor argument value of type [com.accesspointinc.salespoint.business.quote.mapper.internet.AbstractGenericMapper$MyType] to required type [java.lang.String]:
Failed to convert value of type 'com.accesspointinc.salespoint.business.quote.mapper.internet.AbstractGenericMapper$MyType' to required type 'java.lang.String'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.accesspointinc.salespoint.business.quote.mapper.internet.AbstractGenericMapper$MyType] to required type [java.lang.String]: no matching editors or conversion strategy found
以下是相关的构造函数:
public EOFSymmetricQuoteMapper(Service1 service1,
Service2 service2,
Service3 service3,
AnotherMapper revenueMapper,
boolean onNet,
String pricingSourceString) {
super(service1, service2, service3, revenueMapper, onNet, pricingSourceString);
}
public AbstractEOFQuoteMapper(Service1 service1,
Service2 service2,
Service3 service3,
AnotherMapper revenueMapper,
boolean onAPINet,
String quotePricingTypeString) {
super(service1, service2, service3, revenueMapper);
this.onNet = onNet;
quotePricingType = QuotePricingType.valueOf(quotePricingTypeString);
错误似乎很简单 - 我们在构造函数中有一个String,但是在bean配置中传递了枚举类型。但是为什么它在生产中工作而不是在测试中 - bean定义是相同的。