我试图模拟下面的行,但它在执行时出错,它说:
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);
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().
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 "";
}
}
我如何模拟它?
最佳答案
您可以在 Mockito 之上使用 PowerMockito。像这样的事情:
PowerMockito.mockStatic(NameOfClass.class);
expect( NameOfClass.nameOfMethod((URL)Mockito.any(),Mockito.anyString())).andReturn(actualOutput);
关于java - 带参数的模拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44062928/