代码之家  ›  专栏  ›  技术社区  ›  Prem Singh Bist

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

  •  0
  • Prem Singh Bist  · 技术社区  · 9 年前

    我使用的是Jmockit 1.8版。当我尝试这样做时:

    @模拟的 标准标准**;在顶级级别,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
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Rogério    9 年前

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

    @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);
    }    
    
        2
  •  0
  •   Prem Singh Bist    8 年前

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

    @模拟会话示例导师会话示例导师;

    final Criteria cr=新的CriteriaImpl(“YourClassName”,会话示例导师);

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