Unit Test for DAO class: Java Spring MVC using Hibernate and H2 database
<p>I am trying to write jUnit test for my DAO class and am getting error when run the following test in eclipse:</p>

<pre><code>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());
    }
}
</code></pre>

<p>Also my DAO implementation class is as follows:</p>

<pre><code>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&lt;Book&gt; getAllBooks() {
        session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("FROM Book");
        List&lt;Book&gt; 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)
}
</code></pre>

<p>I have also created <strong>applicationContext-test.xml</strong> under the folder <strong>src/test/resources</strong> as follows:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;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"&gt;
    &lt;bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
        &lt;property name="driverClassName" value="org.h2.Driver" /&gt;
        &lt;property name="url" value="jdbc:h2:tcp://localhost/~/test" /&gt;
        &lt;property name="username" value="sa" /&gt;
        &lt;property name="password" value="" /&gt;
    &lt;/bean&gt;
    &lt;bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
        &lt;property name="dataSource" ref="dataSource" /&gt;
        &lt;property name="hibernateProperties"&gt;
            &lt;props&gt;
                &lt;prop key="hibernate.Dialect"&gt;
                    org.hibernate.dialect.H2Dialect&lt;/prop&gt;
                &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
                &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
                &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
            &lt;/props&gt;
        &lt;/property&gt;
        &lt;property name="packagesToScan"&gt;
            &lt;list&gt;&lt;value&gt;com.bookstore&lt;/value&gt;&lt;/list&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
    &lt;bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
        &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
    &lt;/bean&gt;
&lt;/beans&gt;
</code></pre>

<p>Now the error I am getting in the JUnit window is <code>NullPointerException</code> at line <code>bookDao.bookInsert(book);</code> of my test:</p>

<p><a href="https://i.stack.imgur.com/OhpBB.png" rel="nofollow noreferrer">JUnit window of Eclipse</a></p>

<p>And in the console I can see the following:</p>

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