簡體   English   中英

Spring Test / JUnit問題-無法加載應用程序上下文

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

我是第一次使用Spring,一定做錯了。 我有一個包含多個Bean實現的項目,現在我試圖用Spring Test和JUnit創建一個測試類。 我正在嘗試使用Spring Test將定制的bean注入測試類。

這是我的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>

從測試類中注入MyUuidFactory實例以及以下代碼:

private UuidFactory uuidFactory;

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

但是,當我去運行測試(在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

有趣的是,Eclipse / Spring XML編輯器顯示的錯誤是我錯誤拼寫了任何類型或idrefs。 如果我將bean留在里面,但注釋掉依賴項注入,則一切正常,直到我在運行測試時得到NullPointerException ...這才有意義。

嘗試<ref/>而不是<idref/>

“ idref元素只是將容器中另一個bean的id傳遞給<constructor-arg/><property/>元素的一種防錯方法。”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM