gpt4 book ai didi

java - org.mockito.exceptions.misusing.InvalidUseOfMatchersException :Misplaced argument matcher

转载 作者:行者123 更新时间:2023-12-02 05:38:11 27 4
gpt4 key购买 nike

我的代码

@Path("/apiajax/*/*")
@GET
public String handleFooJson(@Context UriInfo context) {
return null;
}

我的测试是

Mockito.when(Bar.handleFooJson(Mockito.any(UriInfo.class))).thenReturn(null);

我遇到错误

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced argument matcher detected here: Mockito.when(Bar.handleFooJson(Mockito.any(UriInfo.class))).thenReturn(null); 

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"))

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().

我不明白这个描述。我如何正确测试它?

最佳答案

我没有足够的观点来添加评论,但我想补充一点,请检查您是否正在 mock 某些可以 mock 的方法。请阅读错误中的内容,

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().

静态方法也不能被模拟,我认为这里就是这种情况。

Mockito.when(Bar.handleFooJson(Mockito.any(UriInfo.class))).thenReturn(null);

Bar 看起来像一个类而不是一个对象。

关于java - org.mockito.exceptions.misusing.InvalidUseOfMatchersException :Misplaced argument matcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24771657/

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