使用Mockito测试方法时遇到问题。请检查JunitTestCaseClass的testMethodToBeTested()方法,该方法必须处理第三方类的静态方法调用。
class ClasssToBeTested{
public String methodToBeTested() {
String result = ThirdPartyUtilClass.methodToBeCall();
return result;
}
}
class ThirdPartyUtilClass{
public static String methodToBeCall(){
return "OK";
}
}
// JunitTestCase which will test method "methodToBeTested()" of ClasssToBeTested class
class JunitTestCaseClass{
@InjectMocks
private ClasssToBeTested classsToBeTested;
@Test
public void testMethodToBeTested() {
//How to handle ThirdPartyUtilClass.methodToBeCall(); statement in unit testing
String result = classsToBeTested.methodToBeTested();
Assert.assertNotNull(result);
}
}
请帮助并提前致谢。
我认为这是您无法解决问题的答案:https : //github.com/mockito/mockito/wiki/FAQ
Mockito的局限性是什么
Mockito 2.x的特定限制
Requires Java 6+
Cannot mock static methods
Cannot mock constructors
Cannot mock equals(), hashCode().
首先,您不应该模拟那些方法。其次,Mockito定义并依赖于这些方法的特定实现。重新定义它们可能会破坏Mockito。仅在Objenesis支持的VM上可以进行模拟。不用担心,大多数VM都可以正常工作。监视真正的实现中通过OuterClass引用外部类的真正方法,这是不可能的。别担心,这是极为罕见的情况。
如果您真的想模拟静态方法,那么PowerMock是您的解决方案。https://github.com/powermock/powermock/wiki/mockito
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句