简体   繁体   中英

Spring Test / JUnit problem - unable to load application context

I am using Spring for the first time and must be doing something wrong. I have a project with several Bean implementations and now I am trying to create a test class with Spring Test and JUnit. I am trying to use Spring Test to inject a customized bean into the test class.

Here is my test-applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=".............">
  <bean
    id="MyUuidFactory"
        class="com.myapp.UuidFactory" 
        scope="singleton" >
    <property name="typeIdentifier" value="CLS" />
  </bean>

  <bean id="ThingyImplTest"
        class="com.myapp.ThingyImplTest"
          scope="singleton">
     <property name="uuidFactory">
        <idref local="MyUuidFactory"   />
     </property>
</bean>
</beans>

The injection of MyUuidFactory instance goes along with the following code from within the test class:

private UuidFactory uuidFactory;

public void setUuidFactory(UuidFactory uuidFactory) {
    this.uuidFactory = uuidFactory;
}

However, when I go to run the test (in Eclipse or command line) I get the following error (stack trace omitted for brevity):

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyImplTest' defined in class path resource [test-applicationContext.xml]:
Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: 
Failed to convert property value of type 'java.lang.String' to required type 'com.myapp.UuidFactory' for property 'uuidFactory'; 
nested exception is java.lang.IllegalStateException: 
Cannot convert value of type [java.lang.String] to required type [com.myapp.UuidFactory] for property 'uuidFactory': 
no matching editors or conversion strategy found

Funny thing is, the Eclipse/Spring XML editor shows errors of I misspell any of the types or idrefs. If I leave the bean in, but comment out the dependency injection, everything work until I get a NullPointerException while running the test...which makes sense.

Try <ref/> not <idref/> .

"The idref element is simply an error-proof way to pass the id of another bean in the container (to a <constructor-arg/> or <property/> element)."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM