DAO类的单元测试:使用Hibernate和H2数据库的Java Spring MVC

时间:2016-12-08 20:24:22

标签: java spring hibernate junit

我正在尝试为我的DAO类编写jUnit测试,并且在eclipse中运行以下测试时遇到错误:

package com.bookstore.dao;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;    
import com.bookstore.domain.Book;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:**/applicationContext-test.xml"})
public class BookDaoImplTest {

    private BookDao bookDao;
    private Book book;

    @Before
    public void setup() {
        book = new Book();
        book.setBookName("Some Book Name");
        book.setBookCategory("Fiction");
        book.setBookPrice(11.11);
        // more properties here (not relevant for the question)
    }

    @Test
    @Rollback(true)
    public void testBookInsert() {
        bookDao.bookInsert(book);  // nullPointer happens at this line!
        final int bookId = book.getBookId();
        Book insertedBook = bookDao.getBookById(bookId);    
        Assert.assertEquals(insertedBook.getBookName(), book.getBookName());
    }
}

我的DAO实现类如下:

package com.bookstore.dao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import com.bookstore.domain.Book;
import com.bookstore.exception.BookNotFoundException;

@Repository
@Transactional
public class BookDaoImpl implements BookDao {

    @Autowired
    private SessionFactory sessionFactory;
    private Session session = null;

    public List<Book> getAllBooks() {
        session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("FROM Book");
        List<Book> books = query.list();
        session.flush();
        return books;
    }

    public Book getBookById(int bookId) {
        session = sessionFactory.getCurrentSession();
        Book bookById = (Book) session.get(Book.class, bookId);
        session.flush();
        if (bookById == null) {
            throw new BookNotFoundException(bookId);
        }
        return bookById;
    }

    public void bookInsert(Book book) {
        session = sessionFactory.getCurrentSession();
        session.saveOrUpdate(book);
        session.flush();
    }

    // more methods here (not relevant for the question)
}

我还在 src / test / resources 文件夹下创建了 applicationContext-test.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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:tcp://localhost/~/test" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.Dialect">
                    org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list><value>com.bookstore</value></list>
        </property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

现在,我在JUnit窗口中收到的错误是NullPointerException我测试的bookDao.bookInsert(book);行:

JUnit window of Eclipse

在控制台中我可以看到以下内容:

INFO: HHH000232: Schema update complete
Dec 08, 2016 10:16:41 PM org.springframework.orm.hibernate4.HibernateTransactionManager afterPropertiesSet
INFO: Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@23529fee] of Hibernate SessionFactory for HibernateTransactionManager
Dec 08, 2016 10:16:41 PM org.springframework.context.support.GenericApplicationContext doClose
INFO: Closing org.springframework.context.support.GenericApplicationContext@5dfcfece: startup date [Thu Dec 08 22:16:38 GMT 2016]; root of context hierarchy

2 个答案:

答案 0 :(得分:0)

您的 applicationContext.xml 文件可能不在单元测试的类路径中。尝试将其复制到测试包的资源文件夹中。

或者您已经在@Before安装方法中自己创建了dao,为什么还要使用@Autowired?

答案 1 :(得分:0)

我认为问题在于您在BookDaoImplTest中创建BookDao实例的方式。 BookDao及其中正确创建的引用bean的唯一方法是通过依赖注入(例如使用@Autowired)。如果您使用“new”运算符或通过模拟它手动创建它,“sessionFactory”之类的字段可能为null(如果您不手动设置它们)。