1

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.

1 Answer 1

3

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)."

Sign up to request clarification or add additional context in comments.

2 Comments

That got past the 'unable to create application context' error, but landed me a NullPointerException inside the test. Looks like Spring didn't inject the dependency.
I needed to put "@Resource" annotation on the field within the test class. That with the use of "ref" instead of "idref" solved the problem. As a new Spring developer, I have to say the documentation on this particular point is really bad. It makes it sound like idref is more of a "typesafe" way to reference a bean when really it is a way to reference the string name of a bean. Very confusing....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.