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 설정 파일 :
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?
: 내가 그것을 실행할 때
<?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를로드 오류가 발생 여기
내 테스트 클래스또한 @Required + test1에 대한 setter를 추가하려했지만 동일한 오류가 발생했습니다.
어떻게 될지 아십니까?
감사
Orid에서
당신은'testDao' 빈을 정의 할 필요가 없습니다. Spring은'@ ContextConfiguration' 설정을 사용하여'DaoImpl'을'testDao'에 삽입 할 것입니다. Spring 컨텍스트 설정에서 테스트 클래스 빈을 제거하십시오. –
예, 해결책입니다! Orid 감사합니다! – Lombric