简体   繁体   中英

How to unit test a class that implements ServletContextListener which is responsible of initialising EntityManagerFactory?

So I have this implementation:

package biz.tugay.books10Aug.dao;
/* User: koray@tugay.biz Date: 10/08/15 Time: 22:22 */

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class PersistenceUtil {

    private static EntityManagerFactory entityManagerFactory;

    public static void initalizeEntityManagerFactory() {
        if (entityManagerFactory == null || !entityManagerFactory.isOpen()) {
            entityManagerFactory = Persistence.createEntityManagerFactory("bookshop");
        }
    }

    public static EntityManagerFactory getEntityManagerFactory() {
        return entityManagerFactory;
    }

    public static EntityManager getEntityManager() {
        if (entityManagerFactory == null || !entityManagerFactory.isOpen()) {
            initalizeEntityManagerFactory();
        }
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        return entityManager;
    }

    public static void closeEntityManagerFactory() {
        entityManagerFactory.close();
    }

}

and when I start my web-app I want the EntityManagerFactory to be ready so I have this Listener:

package biz.tugay.books10Aug.web;
/* User: koray@tugay.biz Date: 10/08/15 Time: 22:24 */

import biz.tugay.books10Aug.dao.PersistenceUtil;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class BookShopServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        PersistenceUtil.initalizeEntityManagerFactory();
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        PersistenceUtil.closeEntityManagerFactory();
    }

}

How do I properly unit test this Listener? This is what I have but I am not sure if it is any good:

package biz.tugay.books10Aug;
/* User: koray@tugay.biz Date: 10/08/15 Time: 22:28 */

import biz.tugay.books10Aug.dao.PersistenceUtil;
import biz.tugay.books10Aug.web.BookShopServletContextListener;
import org.junit.Assert;
import org.junit.Test;

import javax.persistence.EntityManagerFactory;

public class BookShopServletContextListenerTest {

    @Test
    public void shouldCreateEntityManagerFactoryWithoutAnyExceptions() throws Exception {
        BookShopServletContextListener bookShopServletContextListener
                = new BookShopServletContextListener();
        bookShopServletContextListener.contextInitialized(null);
    }

    @Test
    public void testContextDestroyed() throws Exception {
        BookShopServletContextListener bookShopServletContextListener
                = new BookShopServletContextListener();
        bookShopServletContextListener.contextInitialized(null);
        bookShopServletContextListener.contextDestroyed(null);
        EntityManagerFactory entityManagerFactory = PersistenceUtil.getEntityManagerFactory();
        Assert.assertTrue(!entityManagerFactory.isOpen());
    }

}

I would use @PeristentUnit to automatically inject an EntityManagerFactory. In that case no need to create a specific ServletListener and thus testing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM