简体   繁体   English

Spring Test / JUnit问题-无法加载应用程序上下文

[英]Spring Test / JUnit problem - unable to load application context

I am using Spring for the first time and must be doing something wrong. 我是第一次使用Spring,一定做错了。 I have a project with several Bean implementations and now I am trying to create a test class with Spring Test and JUnit. 我有一个包含多个Bean实现的项目,现在我试图用Spring Test和JUnit创建一个测试类。 I am trying to use Spring Test to inject a customized bean into the test class. 我正在尝试使用Spring Test将定制的bean注入测试类。

Here is my test-applicationContext.xml: 这是我的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: 从测试类中注入MyUuidFactory实例以及以下代码:

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): 但是,当我去运行测试(在Eclipse或命令行中)时,出现以下错误(为简便起见,省略了堆栈跟踪):

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. 有趣的是,Eclipse / Spring XML编辑器显示的错误是我错误拼写了任何类型或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. 如果我将bean留在里面,但注释掉依赖项注入,则一切正常,直到我在运行测试时得到NullPointerException ...这才有意义。

Try <ref/> not <idref/> . 尝试<ref/>而不是<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)." “ idref元素只是将容器中另一个bean的id传递给<constructor-arg/><property/>元素的一种防错方法。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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