我没有使用任何框架只使用maven war模块,并希望使用Juit 4 + Powermockito(第一次)测试DAO层。
我的想法是当我致电CustomerDao测试createCustomer时。该方法的第一个陈述如下:
Session session = HibernateManager.getInstance().getSessionFactory().openSession();
我想模拟这个调用,以便我可以使用以下代码提供我在测试类中构造的会话对象:
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", "[email protected]",
"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;
}
}
问题是:
org.hibernate.internal.util.config.ConfigurationException:无法在RESOURCE hibernate.cfg.h2.xml中的第-1行和第-1列执行解组。消息:意外元素(uri:“http://www.hibernate.org/xsd/orm/cfg”,local:“hibernate-configuration”)。预期的要素是
但是如果我删除注释@RunWith(PowerMockRunner.class)
然后我没有得到这个错误。
Session session = HibernateManager.getInstance().getSessionFactory().openSession();
请指导我如何编写单元测试用例来测试DAO层,它可以使用不同的hibernate.cfg.xml
文件。
问题似乎是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 (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();
}
}