java - Junit 4.x + Hibernate 5.x 与 H2 数据库

标签 java spring hibernate junit4 powermockito

我没有使用任何框架,只是使用 maven war 模块,想使用 Juit 4 + Powermockito 测试 DAO 层(第一次)。

我的想法是当我调用 CustomerDao 来测试 createCustomer 时。该方法的第一条语句如下:

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

我想模拟这个调用,以便我可以使用以下代码提供我在测试类中构建的 session 对象:

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

}

问题是:

  1. 当我运行测试类时出现错误:

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>

但是如果我删除注释 @RunWith(PowerMockRunner.class) 那么我没有收到此错误。

  1. 如何模拟 createCustomer() 方法中的方法调用,如下所示: Session session = HibernateManager.getInstance().getSessionFactory().openSession();

请指导我如何编写单元测试用例来测试可以使用不同的 hibernate.cfg.xml 文件的 DAO 层。

最佳答案

问题似乎是 PowerMocks 类加载器。

Unable to parse hibernate.cfg.xml

我让 PowerMock、JUnit4 和 Hibernate 按照相同的原则在 JDK11 中工作,并将以下内容添加到我的类中:

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

全类示例:
org.hibernate hibernate-core 5.4.2.Final(编译)
junit junit:4.12(测试)
net.bytebuddy字节好友1.9.10(编译)
org.powermock powermock-module-junit4 2.0.2(测试)
com.h2database h2 1.4.199(测试)

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

关于java - Junit 4.x + Hibernate 5.x 与 H2 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52659206/

相关文章:

java - JSON转换和应用程序兼容性问题

java - Grails Spring 安全注册需适度

hibernate - 如何在 Hibernate 中执行原子操作?

java - 启动 Intent "silently"

java - 我的 web 应用程序在 context.xml 中找不到 beans

java - 状态 404 - MockMvc 授权 - Spring boot REST 文档

hibernate - 在 Web 应用程序之间共享开源 (Spring/Spring Security/Hibernate/etc) jar

java:服务器(glassfish/tomcat)可以自动调用jsp吗?

java - 使用 NTLM 对 Sharepoint 使用 HttpClient 身份验证机制时出现 HTTP 403 Forbidden

java - 从文本数据库检索图像并显示在按钮上