I'm trying to write a test method that would execute methodA, and want to skip methodB that's being called within methodA. I'm using Mockito's doNothing() to skip methodB, but the methodB is still getting executed.
public class Product extends ParentProduct{
@Override
public void methodB(boolean sync) {
//some code
}
}
This is the Service class that has method methodA that needs to be tested/executed.
public class Service{
public Response methodA(Request request) {
Product product = new Product();
product.init();
//want to skip the below line of code that calls methodB
product.methodB(true);
Response response = product.getResponse();
return response;
}
}
Following is the test method
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Mockito.*;
@RunWith(PowerMockRunner.class)
public class ServiceTest {
@Before
public void setUp() throws IOException {
MockitoAnnotations.initMocks(this);
}
@Mock
private Product product;
@Test
public void testServiceProduct() throws Exception {
Service service = new Service();
Request request = new Request();
doNothing().when(product).methodB(any(Boolean.class));
Response response = service.methodA(request);
Assert.assertTrue(response.getMessage().equals("202"));
}
}
How do I ensure that methodB is skipped and all other lines in methodA are executed? Why is doNothing() not working here? Also I've tried the solution's listed here: Mockito doNothing doesn't work when a Method called with void method call inside Why are my mocked methods not calld when executing a unit test? but this didn't work for me