[英]Mocking of Singleton Public/Void method failing with PowerMockito
So here is the Scenario : 所以这是场景:
public class classA implements classRandom {
public void methodA(arg1, arg2) {
POJOb objB;
objB = SingletonClass_Instance().singletonMethodA(arg1);
}
public SingletonClass SingletonClass_Instance() {
return SingletonClass.getInstance();
}
}
public class SingletonClass {
public objB singletonMethodA(final String arg1) {
return randomObj;
}
}
I am writing a unit test for methodA in Class A. For this i have written the following stub code: 我正在为类A中的methodA编写单元测试。为此,我编写了以下存根代码:
SingletonClass mockSingletonClass = PowerMockito.mock(SingletonClass.class);
POJOb = PowerMockito.mock(POJOb.class);
Mockito.when(mockSingletonClass.singletonMethodA(Mockito.anyString())).thenReturn(POJOb);
Now immediately after this code, i have a code: 现在在此代码之后,我有一个代码:
POJOb objB = classA_Object.methodA(arg1, arg2);
This code fails as soon as it reaches: 此代码一到达就会失败:
**methodA --> SingletonClass_Instance().singletonMethodA**
I have already used the following Powermockito procedures: 我已经使用了以下Powermockito过程:
1) import PM libs
2) Added @PrepareForTest({SingletonClass.class, POJOb.class})
Still i am unable to mock the Public/Void methods of SingletonClass. 我仍然无法模拟SingletonClass的Public / Void方法。 They are always coming out to be null.
他们总是出来为空。
You'll need to do something like this. 您需要执行以下操作。
PowerMockito.mockStatic(SingletonClass.class);
SingletonClass mockSingleton = PowerMockito.mock(SingletonClass.class);
PowerMockito.when(SingletonClass.getInstance()).thenReturn(mockSingleton);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.