I'm trying to doNothing() when a final void method is called, but the doNothing() call is calling the actual method instead.
SwitchWorker class
public class SwitchWorker {
public boolean switchMethod(String id){
this.methodA(id);
return true;
}
public void methodA(String id){
final Switch switchObj = Switch.getSwitchInstance();
switchObj.finalVoidMethod(id);
// XYZ......
}
}
Switch Class
public final class Switch {
private static final Switch INSTANCE = new Switch();
private Switch() { //Private Constructor
super();
}
public static Switch getSwitchInstance() {
return INSTANCE;
}
public final synchronized void finalVoidMethod(String param) {
// Do Something
}
}
Test code:
@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("com.application.switching.Switch")
public class SwitchWorkerTest {
@Test
public void test() throws Exception {
SwitchWorker swObj = new SwitchWorker();
final String channelId = "0001";
Switch switchMock = mock(Switch.class);
PowerMockito.mockStatic(Switch.class);
when(Switch.getSwitchInstance()).thenReturn(switchMock);
doNothing().when(switchMock).finalVoidMethod(channelId);
boolean result = swObj.switchMethod(channelId);
assertTrue(result);
}
Am I missing anything for the doNothing() method?
"doNothing().when(switchMock).finalVoidMethod(channelId);"
doNothing() is calling the actual finalVoidMethod() instead of mocking. But, switchMock is a Mocked object.
I've looked at almost every question on stack overflow regarding PowerMockito, mocking static methods, and doNothing methods, I'm not sure if I'm missing anything as I've followed most examples to build the code. I've added @PrepareForTest too but later removed it.
Thank you.
edit: I snipped the code to be more abstract and changed slightly to fit @kswaughs comment
public boolean mockedMethod(final String input) {
System.out.println("0----------------------");
final Switch switchObj = Switch.getSwitchInstance();
System.out.println("1:switchObj: "+ switchObj);
switches.refresh("input");
System.out.println("2----------------------");
return true;
}
With test code:
@RunWith(PowerMockRunner.class)
@PrepareForTest({Switch.class, SwitchWorker.class})
public class SwitchWorkerTest {
@Test
public void test() throws Exception {
SwitchWorker swObj = new SwitchWorker();
Switch switchMock = mock(Switch.class);
PowerMockito.mockStatic(Switch.class);
when(Switch.getSwitchInstance()).thenReturn(switchMock);
boolean result = swObj.mockedMethod("0001");
assertTrue(result);
returns .
0----------------------
1:switchObj: Mock for Switch, hashCode: 1659309731
java.lang.NullPointerException
at com.application.switching.Switch.refresh(Switch.java:238)
at com.application.switching.SwitchWorker$1.run(SwitchWorker.java:51)
After changing all the methods to use PowerMockito, I realized that the mock object itself was initialised using Mockito.
Changing:
Switch switchMock = mock(Switch.class);
to (or changing the imports):
Switch switchMock = PowerMockito.mock(Switch.class);
fixed my problem.
Thanks to the people who contributed (The small things like this are my bane)