首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Powermock在模拟私有方法时抛出InvalidUseOfMatchersException

Powermock在模拟私有方法时抛出InvalidUseOfMatchersException
EN

Stack Overflow用户
提问于 2021-07-01 15:12:17
回答 2查看 45关注 0票数 0

我是Powermock的新手。昨天我看了一个关于使用Powermock的视频,我遵循了他们的代码,下面有一个演示测试程序。当我运行测试时

代码语言:javascript
运行
复制
package myPackage;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class Test1{
    @Test
    public void testPrivateMethod() throws Exception {
        DemoService mockDemoService = PowerMockito.spy(new DemoService());
        PowerMockito.when(
                mockDemoService,
                "privateMethod",
                ArgumentMatchers.any(String.class)
        ).then(invocation -> invocation.getArgument(0) + "_private_mock");
        Assert.assertEquals(mockDemoService.dependentMethod("LoveTest"), "3000LoveTest_private_mock");
    }
}

class DemoService {
    private String privateMethod(String input) {
        return input + "_private";
    }
    public String dependentMethod(String input) {
        return 3000 + privateMethod(input);
    }
}

它在下面的日志中抛出异常

代码语言:javascript
运行
复制
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced or misused argument matcher detected here:

-> at myPackage.Test1.testPrivateMethod(Test1.java:19)

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

This message may appear after an NullPointerException if the last matcher is returning an object 
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
    when(mock.get(any())); // bad use, will raise NPE
    when(mock.get(anyInt())); // correct usage use

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().
Mocking methods declared on non-public parent classes is not supported.

我有一些关于Mockito的基础知识,代码有点类似于Mockito单元测试(Mockito.spy和Mockito.when),但并不像预期的那样工作。我不知道代码出了什么问题,为什么会抛出异常InvalidUseOfMatchersException。请帮帮我,谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-01 15:16:46

您的代码缺少@PrepareForTest。Powermock必须操作目标mocking类的字节码,所以您必须提供它的路径。

代码语言:javascript
运行
复制
@RunWith(PowerMockRunner.class)
@PrepareForTest(DemoService.class)
public class Test1{
    ...
票数 1
EN

Stack Overflow用户

发布于 2021-07-01 15:20:49

这里使用ArgumentMatchers.any(String.class)代替ArgumentMatchers.anyString()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68205800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档