简体   繁体   English

如何使用 mockito 模拟静态方法?

[英]How to mock static methods with mockito?

I am using mockito for the Junits我正在为 Junits 使用 mockito

I know mockito donot mock static methods but is there a way around for this instead of using powermock?我知道 mockito 不会模拟静态方法,但是有没有办法解决这个问题而不是使用 powermock?

Thanks谢谢

Possible workaround would be to encapsulate static methods in real instance I think.我认为可能的解决方法是在实际实例中封装静态方法。 Or real instance behind the static method.或者静态方法背后的真实实例。

Though that would mean you'll have to modify your production code.虽然这意味着您必须修改您的生产代码。

Honestly if you ask yourself this question now, you are testing your code too late in the development process.老实说,如果您现在问自己这个问题,那么您在开发过程中测试代码为时已晚。 (Now evangelizing ;)) If you were practicing TDD, you would have noticed this issue early, and tweaked your design early to be fully testable with classic testing software. (现在传福音;))如果您正在练习 TDD,您会很早就注意到这个问题,并尽早调整您的设计,以便使用经典测试软件进行全面测试。

I personally use the "pain metrics" when practicing TDD to see if my design is ok or not.我个人在实践 TDD 时使用“痛苦指标”来查看我的设计是否合适。 Of course everything depends on the context, but usually this is a good indicator of good design (at least for me and some others).当然,一切都取决于上下文,但通常这是良好设计的一个很好的指标(至少对我和其他一些人来说)。

So my advice get rid of these static methods or revise your design to not be dependent on static method mocking.因此,我的建议是摆脱这些静态方法或修改您的设计,使其不依赖于静态方法模拟。

Cheers干杯

Not sure what "way around" you are looking for.不确定您正在寻找什么“绕过”。 A lot of people use both Mockito and Powermock together and I've not heard any horror-stories about incompatibilities or conflicts.很多人同时使用 Mockito 和 Powermock,我还没有听说过任何关于不兼容或冲突的恐怖故事。 Just use Powermock in those instances where you need to mock static methods and you should be ok.只需在需要模拟静态方法的情况下使用 Powermock 就可以了。

Or refactor to not use static methods in a way that requires mocking.或者重构为不以需要模拟的方式使用静态方法。

No i think there isn't any way to do this withour PowerMock.不,我认为没有任何方法可以使用我们的 PowerMock 做到这一点。

But you could break the dependency on this static methods, by introducing an adapter .但是你可以通过引入一个适配器来打破对这个静态方法的依赖。

Mockito supports mocking static methods since version 3.4.0. Mockito 从 3.4.0 版本开始支持模拟静态方法。 And it does the job better than PowerMock by limiting the scope of the static method mock.通过限制静态方法模拟的范围,它比 PowerMock 做得更好。

To use this feature, you must enable Mockito's inline mock maker by adding the following file into the test-classpath:要使用此功能,您必须通过将以下文件添加到 test-classpath 中来启用Mockito 的内联模拟生成器

/mockito-extensions/org.mockito.plugins.MockMaker

The contents of the file must be the following single line:文件的内容必须是以下单行:

mock-maker-inline

When you've done this, you can mock final classes and static methods.完成此操作后,您可以模拟最终类和静态方法。

Here an example of how to mock static methods with and without arguments :这是一个如何模拟带参数和不带参数的静态方法的示例:

public class Foo {
    public static String voidMethod() {
        return "voidMethod-return";
    }

    public static String intMethod(int x) {
        return "intMethod-return " + x;
    }
}

...

// Prepare class 'Foo' for static mocking, for the current thread: 
try (MockedStatic<Foo> mocked = Mockito.mockStatic(Foo.class)) {

        //////////
        // Mock 'Foo.voidMethod()' to return "voidMethod-mock":
        mocked.when(Foo::voidMethod).thenReturn("voidMethod-mock");

        assertEquals("voidMethod-mock", Foo.voidMethod());
        mocked.verify(Foo::voidMethod);


        //////////
        // Mock 'Foo.intMethod()' to return "intMethod-mock":
        mocked.when(() -> Foo.intMethod(10)).thenReturn("intMethod-mock 10");

        assertEquals("intMethod-mock 10", Foo.intMethod(10));
        mocked.verify(() -> Foo.intMethod(10));

} // MockedStatic.close() releases the static mock.

// The original static method is 'restored' here: 
assertEquals("voidMethod-return", Foo.voidMethod());
assertEquals("intMethod-return 10", Foo.intMethod(10));

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

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