在休眠状态下使用 JMockit 模拟 session.createCriteria(XYZ.class)

Mocking session.createCriteria(XYZ.class) with JMockit in hiberate

我正在使用 Jmockit 1.8 版。当我尝试做时:

@嘲笑 准则准则**;在顶部的 class 级别,Junit 抛出错误:

代码:

 @Mocked
    SessionFactory sessionFactory;

    @Mocked
    Session session;

    @Mocked
    Transaction transaction;
    @Mocked
    Query query;

    @Mocked 
   Criteria criteria;
@Test
    public void getAllTransformationTest(){
        new NonStrictExpectations() {
            {
                sessionFactory.openSession();
                returns(session);

                session.createCriteria((Class) any);
                returns(criteria);
                criteria.add((SimpleExpression) any);

                criteria.list();
                returns(transformationCore.getListOfTransformations());

                session.close();
            }
        };

        List<TransformationResponse> result = transformation.getAllTransformation();
        assertTrue(result.size() > 0);
    }

任何人都有模拟休眠条件的解决方案 Api。我该怎么做?

java.lang.ExceptionInInitializerError
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at java.lang.Class.newInstance0(Class.java:374)
    at java.lang.Class.newInstance(Class.java:327)
    at mockit.internal.util.ConstructorReflection.newInstanceUsingDefaultConstructor(ConstructorReflection.java:166)
    at mockit.internal.expectations.mocking.BaseTypeRedefinition.createNewMockInstanceFactoryForInterface(BaseTypeRedefinition.java:118)
    at mockit.internal.expectations.mocking.BaseTypeRedefinition.createMockedInterfaceImplementationAndInstanceFactory(BaseTypeRedefinition.java:84)
    at mockit.internal.expectations.mocking.BaseTypeRedefinition.redefineType(BaseTypeRedefinition.java:55)
    at mockit.internal.expectations.mocking.TypeRedefinition.redefineType(TypeRedefinition.java:47)
    at mockit.internal.expectations.mocking.SharedFieldTypeRedefinitions.redefineTypeForMockField(SharedFieldTypeRedefinitions.java:60)
    at mockit.internal.expectations.mocking.FieldTypeRedefinitions.redefineFieldType(FieldTypeRedefinitions.java:48)
    at mockit.internal.expectations.mocking.FieldTypeRedefinitions.redefineFieldTypes(FieldTypeRedefinitions.java:38)
    at mockit.internal.expectations.mocking.SharedFieldTypeRedefinitions.redefineTypesForTestClass(SharedFieldTypeRedefinitions.java:43)
    at mockit.integration.internal.TestRunnerDecorator.handleMockFieldsForWholeTestClass(TestRunnerDecorator.java:110)
    at mockit.integration.internal.TestRunnerDecorator.updateTestClassState(TestRunnerDecorator.java:32)
    at mockit.integration.junit4.internal.JUnit4TestRunnerDecorator.handleMockingOutsideTestMethods(JUnit4TestRunnerDecorator.java:104)
    at mockit.integration.junit4.internal.JUnit4TestRunnerDecorator.invokeExplosively(JUnit4TestRunnerDecorator.java:36)
    at mockit.integration.junit4.internal.MockFrameworkMethod.invokeExplosively(MockFrameworkMethod.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at mockit.internal.util.MethodReflection.invokeWithCheckedThrows(MethodReflection.java:112)
    at mockit.internal.mockups.MockMethodBridge.callMock(MockMethodBridge.java:85)
    at mockit.internal.mockups.MockMethodBridge.invoke(MockMethodBridge.java:44)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.j

我试过了,但无法重现故障。以下简化(但功能等效)的测试版本对我来说效果很好:

@Mocked SessionFactory sessionFactory;
@Mocked Criteria criteria;

@Test
public void exampleTestMockingHibernateCriteriaAPI() {
    final List<String> expectedResults = asList("ab", "cde");
    new Expectations() {{ criteria.list(); result = expectedResults; }};

    // From the code under test:
    Session s = sessionFactory.openSession();
    Criteria c = s.createCriteria(String.class);
    c.add(Restrictions.eq("tst", 123));
    List<?> result = c.list();

    assertEquals(expectedResults, result);
}    

我可以使用下面的 API 找到解决方案。而不是 Mocking Criteria api,而是创建 Criteria API 的真实对象,然后将此对象作为参数传递给 nonStrictExpectation API 参数并模拟从 criteria API 进行的后续调用如下例:

@Mocked SessionImplementor sessionImplementor;

最终标准 cr = new CriteriaImpl("YourClassName", sessionImplementor);

new NonStrictExpectations(cr) {
        {
            sessionFactory.getCurrentSession();
            returns(session);
            session.createCriteria((Class) any);
            returns(cr);
            cr.list();
            returns(getListOfInstance());
        }
    };

    List<MyClass> data = obj.getList(params);
    assertTrue(data.size() > 0);