如何模拟通过PowerMock返回void的静态方法? [英] How do I mock a static method that returns void with PowerMock?

查看:1983
本文介绍了如何模拟通过PowerMock返回void的静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一些静态util方法,其中一些只是传递或引发异常.关于如何模拟具有除void以外的返回类型的静态方法,有很多示例.但是,我该如何模拟将void返回到"doNothing()"的静态方法?

I have a few static util methods in my project, some of them just pass or throw an exception. There are a lot of examples out there on how to mock a static method that has a return type other than void. But how can I mock a static method that returns void to just "doNothing()"?

非无效版本使用以下代码行:

The non-void version uses these lines of codes:

@PrepareForTest(StaticResource.class)

...

PowerMockito.mockStatic(StaticResource.class);

...

Mockito.when(StaticResource.getResource("string")).thenReturn("string");

但是,如果将其应用于返回voidStaticResources,则编译器将抱怨<​​c3>不适用于void ...

However if applied to a StaticResources that returns void, the compile will complain that when(T) is not applicable for void...

有什么想法吗?

一种解决方法可能是让所有静态方法成功返回一些Boolean,但我不喜欢这种解决方法.

A workaround would probably be to just have all static methods return some Boolean for success but I dislike workarounds.

推荐答案

您可以像在真实实例上使用Mockito一样进行操作.例如,您可以链接存根,以下行将使第一个调用不执行任何操作,然后对getResources的第二个调用和以后的调用将引发异常:

You can do it the same way you do it with Mockito on real instances. For example you can chain stubs, the following line will make the first call do nothing, then second and future call to getResources will throw the exception :

// the stub of the static method
doNothing().doThrow(Exception.class).when(StaticResource.class);
StaticResource.getResource("string");

// the use of the mocked static code
StaticResource.getResource("string"); // do nothing
StaticResource.getResource("string"); // throw Exception

感谢马特·拉赫曼(Matt Lachman)的评论,请注意,如果在模拟创建时未更改默认答案,则模拟默认情况下将不执行任何操作.因此,编写以下代码等效于不编写代码.

Thanks to a remark of Matt Lachman, note that if the default answer is not changed at mock creation time, the mock will do nothing by default. Hence writing the following code is equivalent to not writing it.

doNothing().doThrow(Exception.class).when(StaticResource.class);
StaticResource.getResource("string");

尽管如此,对于阅读该测试的同事来说,您对于此特定代码没有任何期望可能会很有趣.当然,可以根据对测试的可理解性的看法进行调整.

Though that being said, it can be interesting for colleagues that will read the test that you expect nothing for this particular code. Of course this can be adapted depending on how is perceived understandability of the test.

顺便说一句,以我的拙见,如果您要编写新代码,则应避免模拟静态代码.在Mockito,我们认为这通常是不良设计的提示,它可能导致可维护性差的代码.尽管现有的遗留代码又是另外一回事了.

By the way, in my humble opinion you should avoid mocking static code if your crafting new code. At Mockito we think it's usually a hint to bad design, it might lead to poorly maintainable code. Though existing legacy code is yet another story.

通常来说,如果您需要模拟私有方法或静态方法,则此方法会执行过多操作,应将其外部化到将注入测试对象中的对象中.

Generally speaking if you need to mock private or static method, then this method does too much and should be externalized in an object that will be injected in the tested object.

希望有帮助.

致谢

这篇关于如何模拟通过PowerMock返回void的静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆