Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit4.x+;Hibernate 5.x与H2数据库_Java_Spring_Hibernate_Junit4_Powermockito - Fatal编程技术网

Java JUnit4.x+;Hibernate 5.x与H2数据库

Java JUnit4.x+;Hibernate 5.x与H2数据库,java,spring,hibernate,junit4,powermockito,Java,Spring,Hibernate,Junit4,Powermockito,我没有使用任何框架,只是使用maven war模块,我想使用Juit 4+Powermockito(第一次)测试DAO层 我的想法是当我打电话给CustomerDao测试createCustomer时。该方法的第一句话如下: Session session = HibernateManager.getInstance().getSessionFactory().openSession(); 我想模拟此调用,以便提供我在测试类中使用以下代码构造的会话对象: import org.hibernat

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

}
问题是:

  • 当我运行我的测试类时,我得到一个错误:
  • org.hibernate.internal.util.config.ConfigurationException:无法 在资源中的行号-1和列-1处执行解组 hibernate.cfg.h2.xml。消息:意外元素 (uri):, 本地:“休眠配置”)。预期的要素是

    但是如果我删除注释
    @RunWith(PowerMockRunner.class)
    那我就不会犯这个错误了

  • 如何模拟createCustomer()方法中的方法调用,如下所示: 会话会话=HibernateManager.getInstance().getSessionFactory().openSession()

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

    问题似乎是PowerMocks类加载器

    我让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 byte buddy 1.9.10(编译)

    org.powermock-powermock-module-junit4 2.0.2(测试)

    com.h2数据库h2 1.4.199(测试)


    模拟会话来测试DAO基本上是无用的。DAO的职责是使用查询从数据库获取数据。因此,基本上,您需要检查的是查询是否正确,并从数据库返回正确的值。模拟会话只会检查您是否正在执行查询。您仍然不知道查询是否正确,以及它是否返回正确的数据。不要使用mock来测试DAO。相反,在测试开始时用测试数据填充数据库,调用DAO并检查它是否返回了正确的数据。模拟正在测试的DAO更不正确:您甚至不会执行一行代码,但是要测试Mockito是否工作正常。感谢Prashant的响应,但在我的例子中,有标准方法用于保存对象,如session。save()我想在测试用例中使用我的断言来检查对象是否正确持久化。这正是你不应该模拟DAO或Hibernate会话的原因。如果你嘲笑它,它的所有方法都会被嘲笑,什么也不做。所以你不能检查他们是否正确地完成了他们的工作。我的名字是JB Nizet,不是Prashant。啊!很抱歉,非常感谢您的回复。你能告诉我,如果我想测试我的凝乳操作,那么应该采取什么方法。因为如果我不模拟会话,那么如果我从测试类调用方法,它将在数据库中创建我不想要的条目,这就是为什么我要使用内存数据库H2,如果我错了,请纠正我
    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();
        }
    }