带参数的模拟方法 [英] Mock method with parameters

查看:59
本文介绍了带参数的模拟方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟以下行,但在执行时出现错误,它说:

I am trying to mock the below line but it gives an error while executing it, it says:

此处检测到错位的参数匹配器:

Misplaced argument matcher detected here:

您不能在验证或存根之外使用参数匹配器.

You cannot use argument matchers outside of verification or stubbing.

参数匹配器的正确使用示例:

Examples of correct usage of argument matchers:

when(mock.get(anyInt())).thenReturn(null);

when(mock.get(anyInt())).thenReturn(null);

doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());

doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());

verify(mock).someMethod(contains("foo"))

verify(mock).someMethod(contains("foo"))

此外,这个错误可能会出现,因为你使用了参数匹配器无法模拟的方法.以下方法不能存根/验证:final/private/equals()/hashCode().

Also, this error might show up because you use argument matchers with methods that cannot be mocked. Following methods cannot be stubbed/verified: final/private/equals()/hashCode().

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:代码行是:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: The line of code is:

PowerMockito.mockStatic(NameOfClass.class);
expect( NameOfClass.nameOfMethod((URL)Mockito.any(),Mockito.anyString())).andReturn(actualOutput);

这个类有点像这样:

public class SomeClass {
    public static String method(String URL, String str) {
    //functioning
        return "";
    }
}

我该如何模拟它?

推荐答案

我无法更改代码中的内容.....它是旧代码并且在很多地方都在使用 – NealGul

i can't change things in my code.....it's an old code and getting used at many places – NealGul

而不是使用 Powermock:

Instead of using Powermock:

有 3 个简单、快速且安全的步骤:

There are 3 easy, quick and safe steps:

  1. 创建该方法的非静态副本(使用新名称):

  1. create a non-static copy of that method (with a new name):

class StaticMethodRefactoring {
    static int staticMethod(int b) {
        return b;
    }

    int nonStaticMethod(int b) {
        return b;
    }
}

  • 让静态版本调用非静态版本:

  • let the static version call the non-static version:

    class StaticMethodRefactoring {
        static int staticMethod(int b) {
            return new StaticMethodRefactoring(). nonStaticMethod(b);
        }
    
        int nonStaticMethod(int b) {
            return b;
        }
    }
    

  • 使用您的 IDE 的内联方法重构,将静态访问替换为非静态访问.

  • use your IDE's inline Method refactoring to replace the static access with the non-static.

    很可能有一个选项可以删除旧的静态方法版本.如果其他一些项目使用这种静态方法,您可以保留它...

    Most likely there is an option to delete the old static version of the method. If some other projects use this static method you may which to keep it...

    您的 IDE 还在您访问静态方法的地方提供了内联方法功能.这样您就可以根据需要逐步更改为非静态访问.

    Your IDE also provides the inline method feature at the place where you access the static method. This way you can change to non-static access step by step as needed.

    就是这样!

    在当前 IDE 会话中的所有打开项目中,对静态方法的任何访问都会被替换.现在您可以更改您的被测类,以通过构造函数或任何其他 DI 机制注入实例,并使用简单的 mockito-mock 替换测试...

    In all open projects in your current IDE session any access to the static method is replaced. Now you can change your class under test to get the instance injected via constructor or any other DI-mechanism and replace is for the tests with a simple mockito-mock...

    这篇关于带参数的模拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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