Mockito mock在尝试存根包保护方法时调用实际方法实现

编程入门 行业动态 更新时间:2024-10-21 06:05:36
本文介绍了Mockito mock在尝试存根包保护方法时调用实际方法实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用Mockito 1.8.5存根方法,但这样做会调用真正的方法实现(使用作为parm值)会引发异常。

I'm trying to stub a method using Mockito 1.8.5, but doing so calls the real method implementation (with "" as parm values) which throws an exception.

package background.internal; //located in trunk/tests/java/background/internal public class MoveStepTest { @Test public void testMoveUpdate() { final String returnValue = "value"; final FileAttachmentContainer file = mock(FileAttachmentContainer.class); doReturn(returnValue).when(file).moveAttachment(anyString(), anyString(), anyString()); //this also fails //when(file.moveAttachment(anyString(), anyString(), anyString())).thenReturn(returnValue); final AttachmentMoveStep move = new AttachmentMoveStep(file); final Action moveResult = move.advance(1, mock(Context.class)); assertEquals(Action.done, moveResult); } }

我正在尝试模拟的方法看起来像这样。没有最终的方法或类。

The method I'm trying to mock looks like this. There are no final method or classes.

package background.internal; //located in trunk/src/background/internal public class FileAttachmentContainer { String moveAttachment(final String arg1, final String arg2, final String arg3) throws CustomException { ... } String getPersistedValue(final Context context) { ... } }

我传递模拟的类看起来像这样:

And the class I'm passing the mock looks like this:

package background.internal; //located in trunk/src/background/internal public class AttachmentMoveStep { private final FileAttachmentContainer file; public AttachmentMoveStep(final FileAttachmentContainer file) { this.file = file; } public Action advance(final double acceleration, final Context context) { try { final String attachmentValue = this.file.getPersistedValue(context); final String entryId = this.file.moveAttachment(attachmentValue, "attachment", context.getUserName()); //do some other stuff with entryId } catch (CustomException e) { e.log(context); } return Action.done; } }

是什么导致实际实现被调用以及如何我可以阻止它吗?

What is causing the real implementation to be invoked and how can I prevent it?

推荐答案

Mockito代码无法访问您正在模拟的方法。

The method you are mocking is not accessible to the Mockito code.

因为您的测试代码和您测试的代码在同一个包中,编译器允许您以这种方式设置模拟,但在运行时,Mockito库必须尝试访问 moveAttachment ,但它不适用于您的情况。这似乎是错误或已知限制,因为它应该支持这种情况,(事实上,在大多数情况下确实支持它)。

Because your test code and your code under test are in the same package, the compiler lets you set up your mock that way, but at runtime, the Mockito library has to try to access moveAttachment, but it's not working in your case. This appears to be a bug or known limitation in Mockito as it should support that case, (and in fact, does support it in most cases).

最简单的方法是使 moveAttachment 成为公共方法。如果这不是一个选项,那么首先询问你是否想要模拟它。如果真正的方法被调用会发生什么?

The easiest thing to do would be to make moveAttachment a public method. If that is not an option, then first question whether you want to even mock it. What happens if the real method gets called?

最后一个选项是使用 PowerMock 将 moveAttachment 方法视为私有方法并以此方式进行模拟。

The last option is to use PowerMock to treat the moveAttachment method as a private method and mock it that way.

更多推荐

Mockito mock在尝试存根包保护方法时调用实际方法实现

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

发布评论

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

>www.elefans.com

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