[英]mocking a class that has a private constructor?
I need to mock a java class that has a private constructor and only a static create method. 我需要模拟一个具有私有构造函数并且只有静态create方法的java类。
What would be the typical solution in that kind of situation. 在这种情况下,典型的解决方案是什么。
With or without a framework. 有无框架。
You can use PowerMock to mock static methods https://code.google.com/p/powermock/wiki/MockitoUsage , so you would mock the static create method and return whatever you want. 您可以使用PowerMock模拟静态方法https://code.google.com/p/powermock/wiki/MockitoUsage ,以便模拟静态create方法并返回所需的任何内容。 It's easier if the static method returns an interface rather than a class so you can return your own mock implementation of it. 如果静态方法返回一个接口而不是一个类,则会更容易,因此您可以返回自己的模拟实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.