简体   繁体   English

从最终类(实用程序类)模拟私有静态方法

[英]Mocking private static method from a final class (utility class)

I am using Mockito and PowerMock to do some testing to my Java app. 我正在使用Mockito和PowerMock对我的Java应用程序进行一些测试。 I was wondering how I can mock a private static method from a final class. 我想知道如何从最终类中模拟私有静态方法。 I want to simulate an Exception when accessing one method but the only way I have found to do it is if the class can be instantiated and using the spy() method from PowerMock. 我想在访问一个方法时模拟一个Exception ,但是我发现做到这一点的唯一方法是是否可以实例化该类并使用PowerMock中的spy()方法。

Could anyone help/guide me a little? 有人可以帮我一下吗?

Here is an example using PowerMock to mock public static methods . 这是一个使用PowerMock模拟公共静态方法示例

In case of a private method there must be some public static method somewhere to mock that calls this private method, as you can not call it directly. 在使用私有方法的情况下,必须在某处模拟某些public static方法来调用此私有方法,因为您不能直接调用它。 If you are only interested in an exception, you might throw the exception earlier (maybe - many assumptions) 如果您只对异常感兴趣,则可以更早抛出该异常(也许有很多假设)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM