简体   繁体   中英

Junit 4.x + Hibernate 5.x with H2 database

I am not using any framework just using maven war module and want to test the DAO layer using Juit 4 + Powermockito (first time).

My idea is when I call CustomerDao to test createCustomer. First statement of this method is as below:

Session session = HibernateManager.getInstance().getSessionFactory().openSession();

I want to mock this call so that I can provide the session object which I constructed in the test class using following code:

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import com.dao.CustomerDao;

@RunWith(PowerMockRunner.class)

public class CustomerDaoTest {

    private SessionFactory sessionFactory;

    @Mock
    CustomerDao customer=new CustomerDao();

    @Before
    public void setup() {
        sessionFactory = createSessionFactory();
        }

    @Test
    public void CustomerCreateAndDeleteTest() throws Exception {
        // Want to mock here
        int id=customer.createCustomer("Indian Customer", "India", "xyz@pk.com", 
        "234567890", "AB");
        Assert.assertEquals(1, id);
     }

    private SessionFactory createSessionFactory() {
        Configuration configuration = new Configuration().configure("hibernate.cfg.h2.xml");// Using H2 for testing only
        sessionFactory = configuration.buildSessionFactory();
        return sessionFactory;
    }

}

Problem is:

  1. When I run my test class I am getting error:

org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number -1 and column -1 in RESOURCE hibernate.cfg.h2.xml. Message: unexpected element (uri:" http://www.hibernate.org/xsd/orm/cfg ", local:"hibernate-configuration"). Expected elements are <{}hibernate-configuration>

But if I remove annotation @RunWith(PowerMockRunner.class) then I am not getting this error.

  1. How can I mock the method call which is inside the createCustomer() method as below: Session session = HibernateManager.getInstance().getSessionFactory().openSession();

Please guide me how I can write Unit test case to test the DAO layer which can use a different hibernate.cfg.xml file.

The issue appears to be PowerMocks classloader.

Unable to parse hibernate.cfg.xml

I got PowerMock, JUnit4, and Hibernate to work in JDK11 following the same principal, and adding the following to my Class:

@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.hibernate.*"})

Full class example:
org.hibernate hibernate-core 5.4.2.Final (compile)
junit junit:4.12 (test)
net.bytebuddy byte-buddy 1.9.10 (compile)
org.powermock powermock-module-junit4 2.0.2 (test)
com.h2database h2 1.4.199 (test)

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.hibernate.*"})
public class PowerMockHibernateTest {

    private SessionFactory sessionFactory;

    public PowerMockHibernateTest() {
    }

    @Before
    public void setUp() {
        sessionFactory = createSessionFactory();
    }

    @After
    public void tearDown() {
        sessionFactory.close();
    }

    private Session getNewSession() {
        return sessionFactory.openSession();
    }

    @Test
    public void getQuery() {
        Session session = getNewSession();
        session.createNamedQuery("PostEntity.All", PostEntity.class);
    }

    private SessionFactory createSessionFactory() {
        Configuration configuration = new Configuration().configure("hibernate.cfg.h2.xml");
        configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        configuration.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
        configuration.setProperty("hibernate.connection.url", "jdbc:h2:mem:test");
        configuration.setProperty("hibernate.hbm2ddl.auto", "update");
        return configuration.buildSessionFactory();
    }
}

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