JimJay
JimJay

Reputation: 349

Can't inject DAO in Junit test with Spring

I'm trying to use HSQL in-memory database with my Junit tests. I have an entity model:

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;
     ...

And a DAO

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

@Repository
public class UserDaoImpl
...

My test context (under src/test/resources/test-context.xml) in full is:

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

And my test class is:

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() {
    ....

But when I run the test, I get a null pointer exception for the injected UserDaoImpl. I don't know why this is?

Upvotes: 3

Views: 2680

Answers (1)

Michael Wiles
Michael Wiles

Reputation: 21184

You're using the MockitoJunitRunner to run the test - you need to use the SpringJunitRunner (not sure what the exact class name is).

Upvotes: 4

Related Questions