我测试的系统调用静态方法。静态方法属于final类(它是实用程序类)。 测试代码如下:
@Test
public void test() throws Exception {
SUT sut = PowerMockito.spy(new SUT());
PowerMockito.spy(A.class);
stub(method(A.class, "methodOne")).toReturn("CONST_STRING");
PowerMockito.when(A.methodTwo(anyLong(), anyLong()))
.thenReturn(false, true, false); //The problem!
sut.method("arg1");
assertThat(//some assert);
}
我在下面的行中出现间歇性错误。它有时会通过&有时,它会失败。
PowerMockito.when(A.methodTwo(anyLong(), anyLong()))
.thenReturn(false, true, false);
如果失败,则显示以下错误:
[junit] Misplaced argument matcher detected here:
[junit]
[junit] -> at com.project.SUTTest.test(SUTTest.java:40)
[junit] -> at com.project.SUTTest.test(SUTTest.java:40)
[junit]
[junit] You cannot use argument matchers outside of verification or stubbing.
[junit] Examples of correct usage of argument matchers:
[junit] when(mock.get(anyInt())).thenReturn(null);
[junit] doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
[junit] verify(mock).someMethod(contains("foo"))
[junit]
[junit] Also, this error might show up because you use argument matchers with methods that cannot be mocked.
[junit] Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
[junit] Mocking methods declared on non-public parent classes is not supported.
[junit]
[junit] org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
[junit] Misplaced argument matcher detected here:
[junit]
[junit] -> at com.project.SUTTest.test(SUTTest.java:40)
[junit] -> at com.project.SUTTest.test(SUTTest.java:40)
[junit]
[junit] You cannot use argument matchers outside of verification or stubbing.
[junit] Examples of correct usage of argument matchers:
[junit] when(mock.get(anyInt())).thenReturn(null);
[junit] doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
[junit] verify(mock).someMethod(contains("foo"))
[junit]
[junit] Also, this error might show up because you use argument matchers with methods that cannot be mocked.
[junit] Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
[junit] Mocking methods declared on non-public parent classes is not supported.
我的PowerMockito版本 - 1.6.3 怎么了?为什么失败是间歇性的?造成这种不可靠行为的原因是什么?
PowerMockito链接对Spied类
无法可靠地工作