public enum SPHttpClient {
;
private static fun(clientInstance) {
HttpPost postRequest;
/* some processing*/
clientInstance.execute(postRequest);
// I need to mock this execute statement
}
}
在私人方法乐趣上写Junit测试,
如何调用枚举类型中的私人方法,我还需要模拟通过私人方法参数的clientinstance?
SPHttpClient spHttpClient;
final Method method = spHttpClient.getClass().getDeclaredMethod("fun", HttpClient.class);
method.setAccessible(true);
Object actual = method.invoke(spHttpClient, mockHttpClient);
我认为getclass
不适合枚举?
Invoke的签名是invoke(Object obj, Object... args)
。obj
必须是此对象。由于您的方法是静态的,因此没有此对象,您可以通过null,但必须省略它,即以下工作:
final Method method = SpHttpClient.class.getDeclaredMethod("fun", HttpClient.class);
method.setAccessible(true);
Object actual = method.invoke(null, mockHttpClient);