How to mock a void method which is non-static, non-finalThe signature of method is as below. I'm using Powermockito.
public class Name{
public void methodName{
...
...
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({Name.class})
public class TestClass{
@Test
public void testMethodName(){
PowerMockito.doNothing().when(Name.class, methodName);
//some when calls after this and assert later on
}
I want to DO Nothing when methodName is called. the above code is not working. it says methodName cannot be resolved.
if you want to mock a non-static method you need to specify the mock object:
public class Name{
public void methodName{
...
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({Name.class})
public class TestClass{
@Test
public void testMethodName(){
Name myName = PowerMockito.mock(Name.class);
PowerMockito.doNothing().when(myName).methodName();
//some when calls after this and assert later on
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With