在Spring MVC项目中使用junit测试dao

时间:2013-07-17 11:57:51

标签: java spring unit-testing junit

我刚开始与春天的旅程,所以我是一个新手。

我正在尝试将测试写入DAO。

当我运行测试时,堆栈跟踪返回:

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)}

看起来我不应该在TestEntityDAO实现之上使用@Autowired。当我删除@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>

1 个答案:

答案 0 :(得分:3)

弹簧不会扫描您的包裹。

您指出:

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

但TestEntityDaoJPA位于pl.com.tt.persistence中。因此,不会扫描此包,也不会创建任何bean。

尝试更改为:

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