简体   繁体   English

私有静态内部类-Powermock

[英]Private static inner class - Powermock

I want to mock private static inner class using Powermock (based on EasyMock). 我想使用Powermock(基于EasyMock)模拟私有静态内部类。 This does not come from production code, it's just a question whether something is possible. 这不是来自生产代码,这只是一个问题是否可行。 I am pretty sure this is bad design, but it's something I'm trying for science. 我很确定这是一个不好的设计,但这是我为科学所做的尝试。

Let's say we have a class with static private inner class: 假设我们有一个带有静态私有内部类的类:

public class Final {
    private static class Inner {
        private final int method () { return 5; }
    }

    public int callInnerClassMethod () {
        return new Inner().method();
    }
}

I would like to mock the Inner class and its method . 我想模拟Inner类及其method

I have come out with the code as follows: 我得出的代码如下:

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

In the code: we get the type of Inner.class and mock it, when a user creates new object of type Inner we say that we return our instance, and when someone calls its method we provide our implementation for it. 在代码中:我们获得Inner.class的类型并对其进行模拟,当用户创建Inner类型的新对象时,我们说我们返回实例,而当有人调用其method我们为其提供实现。

Generally, I am learning about mocking and one can be sure this code proves I don't know what I'm doing. 通常,我正在学习有关模拟的知识,可以肯定的是,这段代码证明了我不知道自己在做什么。 The code does not even compile and I get the following error on the line PowerMock.expectNew(innerType).andReturn(innerType) : 该代码甚至无法编译,并且在PowerMock.expectNew(innerType).andReturn(innerType)行上出现以下错误:

andReturn (capture) in IExpectationSetters cannot be applied to (java.lang.Object) IExpectationSetters中的andReturn(捕获)不能应用于(java.lang.Object)

Is the mocking of private static inner class even possible? 甚至可以嘲笑私有静态内部类吗? I have not found a definitive code example on SO. 我还没有找到关于SO的权威代码示例。

I have managed to get around the compile error by using bare Class innerType = ... instead of Class<?> innerType = ... in my code. 我设法通过在代码中使用裸Class innerType = ...而不是Class<?> innerType = ...来解决编译错误。 It feels wrong, but works. 感觉不对,但是行得通。 I'd grateful if someone explained the difference and how to make it work in the original example. 如果有人在原始示例中解释了差异以及如何使差异生效,我将不胜感激。 There were also some places where I mixed innerType and innerTypeMock . 在某些地方,我混合了innerTypeinnerTypeMock The full, working test code looks as follows: 完整的有效测试代码如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM