私有静态内部 class - Powermock
Private static inner class - Powermock
我想使用 Powermock(基于 EasyMock)模拟 private static inner class。这不是来自生产代码,这只是一个问题是否可行。我很确定这是一个糟糕的设计,但这是我正在为科学而尝试的东西。
假设我们有一个 class 和 static private inner class:
public class Final {
private static class Inner {
private final int method () { return 5; }
}
public int callInnerClassMethod () {
return new Inner().method();
}
}
我想模拟 Inner
class 及其 method
。
我整理出来的代码如下:
Class<?> innerType = Whitebox.getInnerClassType(Final.class, "Inner");
Object innerTypeMock = PowerMock.createMock(innerType);
PowerMock.expectNew(innerType).andReturn(innerType);
PowerMock.expectPrivate(innerType, "method").andReturn(42);
PowerMock.replay(innerTypeMock);
new Final().callInnerClassMethod();
在代码中:我们获取 Inner.class
的类型并模拟它,当用户创建类型为 Inner
的新对象时,我们说我们 return 我们的实例,当有人称其为 method
我们为其提供实现。
一般来说,我正在学习模拟,可以肯定这段代码证明我不知道自己在做什么。该代码甚至无法编译,我在 PowerMock.expectNew(innerType).andReturn(innerType)
:
行收到以下错误
andReturn (capture) in IExpectationSetters cannot be applied to
(java.lang.Object)
private static inner class 的 mocking 甚至可能吗?我还没有在 SO 上找到明确的代码示例。
我通过在我的代码中使用 Class innerType = ...
而不是 Class<?> innerType = ...
设法解决了编译错误。感觉不对,但有效。如果有人解释了差异以及如何使其在原始示例中起作用,我将不胜感激。还有一些地方我把innerType
和innerTypeMock
混在一起了。完整的工作测试代码如下所示:
Class innerType = Whitebox.getInnerClassType(Final.class, "Inner");
Object innerTypeMock = PowerMock.createMock(innerType);
PowerMock.expectNew(innerType).andReturn(innerTypeMock);
PowerMock.expectPrivate(innerTypeMock, "method").andReturn(42);
PowerMock.replayAll();
System.out.println(""+new Final().callInnerClassMethod());
我想使用 Powermock(基于 EasyMock)模拟 private static inner class。这不是来自生产代码,这只是一个问题是否可行。我很确定这是一个糟糕的设计,但这是我正在为科学而尝试的东西。
假设我们有一个 class 和 static private inner class:
public class Final {
private static class Inner {
private final int method () { return 5; }
}
public int callInnerClassMethod () {
return new Inner().method();
}
}
我想模拟 Inner
class 及其 method
。
我整理出来的代码如下:
Class<?> innerType = Whitebox.getInnerClassType(Final.class, "Inner");
Object innerTypeMock = PowerMock.createMock(innerType);
PowerMock.expectNew(innerType).andReturn(innerType);
PowerMock.expectPrivate(innerType, "method").andReturn(42);
PowerMock.replay(innerTypeMock);
new Final().callInnerClassMethod();
在代码中:我们获取 Inner.class
的类型并模拟它,当用户创建类型为 Inner
的新对象时,我们说我们 return 我们的实例,当有人称其为 method
我们为其提供实现。
一般来说,我正在学习模拟,可以肯定这段代码证明我不知道自己在做什么。该代码甚至无法编译,我在 PowerMock.expectNew(innerType).andReturn(innerType)
:
andReturn (capture) in IExpectationSetters cannot be applied to (java.lang.Object)
private static inner class 的 mocking 甚至可能吗?我还没有在 SO 上找到明确的代码示例。
我通过在我的代码中使用 Class innerType = ...
而不是 Class<?> innerType = ...
设法解决了编译错误。感觉不对,但有效。如果有人解释了差异以及如何使其在原始示例中起作用,我将不胜感激。还有一些地方我把innerType
和innerTypeMock
混在一起了。完整的工作测试代码如下所示:
Class innerType = Whitebox.getInnerClassType(Final.class, "Inner");
Object innerTypeMock = PowerMock.createMock(innerType);
PowerMock.expectNew(innerType).andReturn(innerTypeMock);
PowerMock.expectPrivate(innerTypeMock, "method").andReturn(42);
PowerMock.replayAll();
System.out.println(""+new Final().callInnerClassMethod());