【发布时间】:2020-03-04 06:45:15
【问题描述】:
我正在尝试在单元测试中验证在特定配置中未调用静态方法。
因此,我正在使用 PowerMock (powermock-core:2.0.4 & powermock-module-junit4:2.0.4) 及其 Mockito API (powermock-api-mockito2:2.0.4)。
做的时候
PowerMockito.mockStatic(MyClass.class);
serviceUnderTest.methodThatShouldNotCallStaticMethod(arg1, arg2); //service not of type MyClass of course
PowerMockito.verifyStatic(MyClass.class, never());
MyClass.staticMethod(any(), any());
在用
注释的类中的测试方法上@RunWith(PowerMockRunner.class)
@PrepareForTest({MyClass.class})
我收到以下错误:org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type Class and is not a mock!。
我做错了什么以及如何解决?
谢谢
【问题讨论】:
-
除了 any() 之外,一切似乎都很好,最好将其转换为正确的非原始类型或正确的原始类型,如 anyString()...等
-
感谢您的最佳实践!
标签: java android unit-testing powermockito