2013-07-17 2 views
0

방금 ​​스프링으로 여행을 시작 했으므로 초보자입니다.Spring MVC 프로젝트에서 junit으로 테스트하기

DAO에 테스트를 작성하려고합니다.

내가 실행

는 스택 추적 반환을 테스트 : 나는 TestEntityDAO 구현 위에 @Autowired를 사용해서는 안처럼

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pl.com.tt.persistence.TestEntityDaoJPA pl.com.tt.tests.TestPersistenceDAO.testEntityDaoJPA; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [pl.com.tt.persistence.TestEntityDaoJPA] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 

것 같습니다. @Autowire 주석을 삭제하면 스택 추적은 호출 메소드 testEntityDaoJPA.getAll (sql)로 오류를 반환합니다.

이 내 테스트 클래스입니다 :

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
public class TestPersistenceDAO extends AbstractTransactionalJUnit4SpringContextTests{ 

    @Autowired 
    private TestEntityDaoJPA testEntityDaoJPA; 

    @Test 
    public void testDAO(){ 
     String sql = "SELECT r FROM TestEntity r WHERE ROWNUM<200"; 
     testEntityDaoJPA.getAll(sql); 
    } 
} 

내 DAO 클래스 :

@Component 
public class TestEntityDaoJPA implements TestEntityDao { 

    @Autowired 
    @PersistenceContext private EntityManager em; 

    @Transactional 
    public List<TestEntity> getAll(String sql){ 
     TypedQuery<TestEntity> q = em.createQuery(sql, TestEntity.class); 
     List<TestEntity> result = q.getResultList(); 

       return result; 
    } 
} 

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:p="http://www.springframework.org/schema/p" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     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 
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 


    <context:component-scan base-package="pl.com.tt.tests" /> 
    <context:annotation-config/> 
    <tx:annotation-driven/> 
    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
     <property name="url" 
      value="jdbc:oracle:thin:@192.168.80.128:1521:orcl" /> 
     <property name="username" value="findfnorg" /> 
     <property name="password" value="findfnorg" /> 
    </bean> 

    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="pl.com.tt.tests" /> 
     <property name="persistenceProviderClass" 
      value="org.hibernate.ejb.HibernatePersistence" /> 
     <property name="loadTimeWeaver"> 
      <bean 
       class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> 
     </property> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect 
       </prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
    </bean> 



<bean id="transactionManager" 
     class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

</beans> 
+0

그래서, 당신은 Autowired'이 적절한 @ 다음', 당신은 당신의 문맥 인스턴스를 가지고 있는지 확인해야합니다 봄에 의해 생성 된 클래스의 인스턴스를 테스트하려는 경우 그 계급의 여기에있는 REAL 질문은 '192.168.80.128 : 1521 : orcl'에서 데이터베이스를 사용하기위한 JUnit 테스트를 정말로 하시겠습니까? –

답변

3

귀하의 패키지가 스프링에 의해 검색되지 않습니다.

당신은 나타냅니다

<context:component-scan base-package="pl.com.tt.tests" /> 

그러나 TestEntityDaoJPA는 pl.com.tt.persistence입니다. 따라서이 패키지는 스캔되지 않으며 빈이 생성되지 않습니다.

봅니다 변경에 :

<context:component-scan base-package="pl.com.tt.tests,pl.com.tt.persistence" /> 
관련 문제