gpt4 book ai didi

java - Mockito doAnswer 因未知原因抛出 InvalidUseOfMatchersException

转载 作者:太空宇宙 更新时间:2023-11-04 11:49:24 25 4
gpt4 key购买 nike

我想模拟我的 PermissionHostCompat 类的操作 requestPermission(@NonNull String Permission, int requestCode)

public class PermissionHostCompat {

(...)

public void requestPermission(@NonNull String permission, int requestCode) {
requestPermissions(new String[]{permission}, requestCode);
}

public void requestPermissions(@NonNull String[] permissions, int requestCode) {
if (activity != null) {
ActivityCompat.requestPermissions(activity, permissions, requestCode);
} else if (fragment != null) {
FragmentCompat.requestPermissions(fragment, permissions, requestCode);
} else {
fragmentv4.requestPermissions(permissions, requestCode);
}
}

(...)
}

我是这样做的:

@Mock
PermissionHostCompat permissionHostCompat;

//...

Answer<Void> requestPermissionAction = new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
// some code
return null;
}
};
doAnswer(requestPermissionAction).when(permissionHostCompat)
.requestPermission(anyString(), anyInt());

它引发了我的错误:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 2 recorded:

This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));

我不知道为什么。我还尝试了 any(String.class)any(int.class) 以及所有选项之间的组合。

我的导入:

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import staticorg.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;

Mockito 版本:1.10.19

我做错了什么?

最佳答案

尝试一下它是否可以与 Mockito 2 一起使用。但是应该更改导入:

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Matchers.anyString;

关于java - Mockito doAnswer 因未知原因抛出 InvalidUseOfMatchersException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42001493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com