gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:11:17 24 4
gpt4 key购买 nike

这个StackOverflowAnswer对我来说不起作用,所以我重新提出了这个问题。

如何通过仅修改 testClass (EnleadingClassTest.java) 而不是修改实现 ( EnclosureClass.javaInnerClassType.java)?

要求:

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

EnclosureClassTest.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();

}
}

EnclosureClass.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();
}

EnclosureClassTest.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

最佳答案

我认为你需要像这样定义类:

public class EnclosingClass {

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

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

在您的定义中,InnerClass 应低于 EnleadingClass 的实例。当您不使用反射时,请尝试一下如果您想实例化 InnerClass 会发生什么。

可以直接获取构造函数。并像这样传入 EnclosureClass 的实例:

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

}

关于java - 如何使用 PowerMock 和 EasyMock 模拟非静态私有(private)内部 java 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19891659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com