Mockito - 拦截模拟上的任何方法调用

2023-05-02Java开发问题
67

本文介绍了Mockito - 拦截模拟上的任何方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

是否可以以通用方式拦截 mock 上的所有方法调用?

Is it possible to intercept all method invocations on a mock in a generic way?

示例

给定一个供应商提供的类,例如:

Given a vendor provided class such as:

public class VendorObject {

    public int someIntMethod() {
        // ...
    }

    public String someStringMethod() {
        // ...
    }

}

我想创建一个模拟,将 all 方法调用重定向到另一个具有匹配方法签名的类:

I would like to create a mock that re-directs all method calls to another class where there are matching method signatures:

public class RedirectedToObject {

    public int someIntMethod() {
        // Accepts re-direct
    }

}

Mockito 中的 when().thenAnswer() 构造似乎符合要求,但我找不到将任何方法调用与任何参数匹配的方法.无论如何,InvocationOnMock 肯定给了我所有这些细节.有没有通用的方法来做到这一点?看起来像这样的东西,其中 when(vo.*) 被替换为适当的代码:

The when().thenAnswer() construct in Mockito seems to fit the bill but I cannot find a way to match any method call with any args. The InvocationOnMock certainly gives me all these details anyway. Is there a generic way to do this? Something that would look like this, where the when(vo.*) is replaced with appropriate code:

VendorObject vo = mock(VendorObject.class);
when(vo.anyMethod(anyArgs)).thenAnswer(
    new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) {

            // 1. Check if method exists on RedirectToObject.
            // 2a. If it does, call the method with the args and return the result.
            // 2b. If it does not, throw an exception to fail the unit test.

        }
    }
);

在供应商类周围添加包装器以简化模拟不是一种选择,因为:

Adding wrappers around the vendor classes to make mocking easy is not an option because:

  1. 现有代码库太大.
  2. 对性能要求极高的应用程序的一部分.

推荐答案

我想你想要的是:

VendorObject vo = mock(VendorObject.class, new Answer() {
    @Override
    public Object answer(InvocationOnMock invocation) {

        // 1. Check if method exists on RedirectToObject.
        // 2a. If it does, call the method with the args and return the
        // result.
        // 2b. If it does not, throw an exception to fail the unit test.

    }
});

当然,如果你想经常使用这种方法,Answer 不需要匿名.

Of course, if you want to use this approach frequently, no need for the Answer to be anonymous.

来自 文档:这是一个非常高级的功能,通常你不需要它来编写体面的测试.但是在使用遗留系统时它会很有帮助."听起来像你.

From the documentation: "It's quite advanced feature and typically you don't need it to write decent tests. However it can be helpful when working with legacy systems." Sounds like you.

这篇关于Mockito - 拦截模拟上的任何方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13