gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:17 25 4
gpt4 key购买 nike

我没有使用任何框架,只是使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com