如何使用 PowerMock 模拟返回 void 的静态方法?

编程入门 行业动态 更新时间:2024-10-28 10:30:38
本文介绍了如何使用 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");

但是,如果应用于返回 void 的 StaticResources,编译器将抱怨 when(T) 不适用于 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.

一般来说,如果需要mock私有或者静态方法,那么这个方法做的太多,应该具体化在一个对象中,然后注入到测试对象中.

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 的静态方法?

本文发布于:2023-10-31 03:50:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   静态   方法   void   PowerMock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!