如何使用PowerMock和EasyMock模拟非静态私有内部java类?

时间:2013-11-10 15:46:00

标签: java junit mocking easymock powermock

StackOverflowAnswer对我不起作用所以我重新创建了这个问题。

如何通过仅修改testClass( EnclosingClassTest.java )而不是实现()来解决此异常( powermock.reflect.exceptions.ConstructorNotFoundException ) EnclosingClass.java InnerClassType.java )?

要求:

  • 使用PowerMock和EasyMock模拟非静态私有内部java类。

EnclosingClassTest.java:

import java.lang.reflect.Constructor;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ EnclosingClass.class })
public class EnclosingClassTest {

    EnclosingClass cut;

    @Test
    public void testMethod() throws Exception {
        /* Initialization */

        /* Mock Setup */
        Class clazz = Whitebox.getInnerClassType(EnclosingClass.class,
                "InnerClass");
        Constructor constructor = Whitebox.getConstructor(clazz, null);
        InnerClassType innerClass = (InnerClassType) constructor
                .newInstance(null);

        /* Activate the Mocks */
        PowerMock.replayAll();

        /* Test Method */

        /* Asserts */
        PowerMock.verifyAll();

    }
}

EnclosingClass.java:

public class EnclosingClass {

    private class InnerClass implements InnerClassType {
        public InnerClass() { /* do stuff */
        }

        public int innerClassMethod() {
            return 0;
        }
    }
}

InnerClassType.java:

public interface InnerClassType {
    public int innerClassMethod();
}

EnclosingClassTest.testMethod()抛出例外:

org.powermock.reflect.exceptions.ConstructorNotFoundException: Failed to lookup constructor with parameter types [ <none> ] in class EnclosingClass$InnerClass.
    at org.powermock.reflect.internal.WhiteboxImpl.getConstructor(WhiteboxImpl.java:258)
    at org.powermock.reflect.Whitebox.getConstructor(Whitebox.java:158)
    at EnclosingClassTest.testMethod(EnclosingClassTest.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NoSuchMethodException: EnclosingClass$InnerClass.<init>()
    at java.lang.Class.throwNoSuchMethodException(Class.java:286)
    at java.lang.Class.getDeclaredConstructor(Class.java:429)
    at org.powermock.reflect.internal.WhiteboxImpl.getConstructor(WhiteboxImpl.java:250)
    ... 31 more

1 个答案:

答案 0 :(得分:0)

我认为你需要像这样区分这个类:

public class EnclosingClass {

    private static class InnerClass implements InnerClassType {
        public InnerClass() { /* do stuff */
        }

        public int innerClassMethod() {
            return 0;
        }
    }
}

在你的定义中,InnerClass应该是一个EnclosingClass的实例。当您不使用反射时,如果您想要实例化InnerClass,请尝试将要发生的事情。

您可以直接获取构造函数。并传入EnclosingClass的实例,如下所示:

public void testMethod() throws Exception {
        /* Initialization */

        /* Mock Setup */
        Class clazz = Whitebox.getInnerClassType(EnclosingClass.class,
                "InnerClass");

        Constructor[] constructors = clazz.getConstructors();
        Constructor c  = constructors[0];
//        c.setAccessible(true);
        InnerClassType innerClass = (InnerClassType) c
                .newInstance(new EnclosingClass());

        /* Activate the Mocks */
        PowerMock.replayAll();

        /* Test Method */

        /* Asserts */
        PowerMock.verifyAll();

    }