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



我刚刚开始我的Spring之旅,所以我是个新手。

我正在尝试向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>

您的包裹未被spring扫描。

您指示:

<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" />

最新更新