2014-01-25 109 views
0

我在運行junit測試時遇到錯誤。依賴注入錯誤

這裏是我的測試類:

package testdao; 

    import static org.junit.Assert.assertTrue; 

    import java.net.UnknownHostException; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 

import dao.daoImpl.DaoImpl; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath:TestDao-context.xml") 
public class TestDao { 
    @Autowired 
    private DaoImpl test1; 
    @Test 
    public void test() { 
     try { 
      test1.connect(); 
      assertTrue(true); 
     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
      assertTrue(false); 
     } 
    } 
} 

,然後我的xml配置文件:

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <!-- Uncomment and add your base-package here: <context:component-scan base-package="org.springframework.samples.service"/> --> 


    <!-- DAO --> 
    <!-- MongoFactoryBean instance --> 
    <bean id="mongoFactoryBean" class="org.springframework.data.mongodb.core.MongoFactoryBean"> 
     <property name="host" value="127.0.0.1" /> 
     <property name="port" value="27017" /> 
    </bean> 

    <bean id="mongoDbFactory" 
     class="org.springframework.data.mongodb.core.SimpleMongoDbFactory"> 
     <constructor-arg name="mongo" ref="mongoFactoryBean" /> 
     <constructor-arg name="databaseName" value="agence_voyage" /> 
    </bean> 

    <bean id="dao" class="dao.daoImpl.DaoImpl"> 
     <property name="mongoFactory" ref="mongoDbFactory" /> 
    </bean> 


    <!-- Services --> 
    <bean id="service" class="service.serviceImpl.ServiceImpl"> 
     <property name="dao" ref="dao" /> 
    </bean> 



<!-- Tests --> 
    <bean id="testDao" class="testdao.TestDao"> 
     <property name="test1" ref="dao"/> 
    </bean> 

</beans> 

因此,當我運行它,我得到一個ApplicationContext加載錯誤造成的:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testDao' defined in class path resource [TestDao-context.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'test1' of bean class [testdao.TestDao]: Bean property 'test1' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? 

此外,我試圖使用@Required +添加setter爲test1,但我得到了同樣的錯誤。

你知道會發生什麼嗎?

感謝

從ORID
+2

你不需要定義'testDao' bean。 Spring將使用'@ ContextConfiguration'配置將'DaoImpl'注入'testDao'。只需從Spring上下文配置中刪除測試類bean –

+0

是的,這就是解決方案!感謝Orid! – Lombric

回答

0

解決方案: 「你不需要定義testDao豆Spring將使用@ContextConfiguration配置注入DaoImpl到testDao剛剛從Spring上下文配置中刪除測試級bean。 「