【问题标题】:Can't inject DAO in Junit test with Spring无法在使用 Spring 的 Junit 测试中注入 DAO
【发布时间】:2013-07-27 09:56:39
【问题描述】:

我正在尝试在我的 Junit 测试中使用 HSQL 内存数据库。我有一个实体模型:

package com.project.model.db;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="users")
public class User {

 @Id
 @GeneratedValue
 private Integer id;
     ...

还有一个 DAO

package com.project.dao.impl;
...

@Repository
public class UserDaoImpl
...

我的完整测试上下文(在 src/test/resources/test-context.xml 下)是:

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

<context:component-scan base-package="com.project" />

<jdbc:embedded-database id="dataSource" type="HSQL" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.project" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>com.project.model.db.User</value>
        </list>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven />

我的测试课是:

package com.project.dao.impl;

import javax.inject.Inject;
import com.project.model.db.User;
...
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration({ "classpath:/test-context.xml" })
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
public class UserDaoImplTest {

@Inject private UserDaoImpl userDaoImpl;

@Test
public void savesUser() {
    ....

但是当我运行测试时,我得到注入的 UserDaoImpl 的空指针异常。我不知道这是为什么?

【问题讨论】:

    标签: java spring hibernate junit dependency-injection


    【解决方案1】:

    您正在使用 MockitoJunitRunner 运行测试 - 您需要使用 SpringJunitRunner(不确定确切的类名是什么)。

    【讨论】:

    • 我同意,您使用了错误的运行器,因为 Mockito 运行器不理解相同的注释并且无法初始化 Spring 应用程序上下文。 Mockito runner 基本上可以理解 @Mock@InjectMock 注释,但对于使用 spring 的集成测试没有完成
    • 我创建了一个跑步者,它可以同时做...... Spring 和 Mockito。听起来不仅我需要它。
    • 不确定了解这些好处。您是否想要进行单元、集成、验收......测试,但不是同时进行。除非您可以轻松地将模拟添加到现有的 Spring 应用程序上下文中(这在带有蛋糕模式的 Scala 中非常容易;p)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2017-02-10
    • 2012-01-25
    相关资源
    最近更新 更多