Powermock 模拟 void 方法抛出错误

编程入门 行业动态 更新时间:2024-10-27 04:32:43
本文介绍了Powermock 模拟 void 方法抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 Powermockito,mockito 和 TestNG.我的测试类扩展了 PowerMockTestCase.我想模拟一个 void 方法.为此,我使用了以下示例语法,

I am using Powermockito, mockito with TestNG. My test class extends PowerMockTestCase. I want to mock a void method. For that I used following sample syntax,

@PrepareForTest(TestClass.class) class Sample extends PowerMockTestCase{ @BeforeClass public void beforeClass(){ TestClass obj = PowerMockito.spy(TestClass.getInstance()); PowerMockito.doNothing().when(obj).voidMethodName(Matchers.any(Type1.class), Matchers.anyString(), Matchers.any(Type2.class)); }

当我给出这个时,我得到:

When I give this, I get :

FAILED CONFIGURATION: @BeforeClass beforeClass org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36) E.g. thenReturn() may be missing. Examples of correct stubbing: when(mock.isOk()).thenReturn(true); when(mock.isOk()).thenThrow(exception); doThrow(exception).when(mock).someVoidMethod(); Hints: 1. missing thenReturn() 2. you are trying to stub a final method, you naughty developer!

你能告诉我我在这里缺少什么吗?

Can you please let me know what I am missing here?

谢谢

推荐答案

如果void方法不是final方法,就不用PowerMockito了!另外,我不明白你为什么要嘲笑doNoting()".如果你有一个默认的模拟行为,当调用 void 方法时抛出异常,并且对于特定的测试用例你想要覆盖这个行为时,这将被使用.Ib 你的情况,会完全删除这条线.

If the void method is not a final method, you do not need to use PowerMockito! Also, I don't see why you would mock "doNoting()". This would be used if you had a default mocking behavior, throwing an exception when the void method is called, and when for a specific test case you would want to override this behavior. Ib your case, would remove this line completely.

更多推荐

Powermock 模拟 void 方法抛出错误

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

发布评论

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

>www.elefans.com

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