Search code examples
javaunit-testingmockingmockitopowermockito

java PowerMockito ignore method call


In a unit test, how can I ignore a call to a method as shown below?

void methodToBeTested(){
     // do some stuff
     methodToBeSkipped(parameter);
     // do more stuff
}

void methodToBeSkipped{
     // do stuff outside of test scope
}

@Test
void TestMethodToBeTested(){
     TestedClass testedClass = new TestedClass();
     testedClass.methodToBeTested();
     // asserts etc.
}

Solution

  • You don't need for this. You could simply spy the object you're going to test and mock out the method you want to skip:

    @Test
    public void testMethodToBeTested() {
        TestedClass testedClass = Mockito.spy(new TestedClass());
        Mockito.doNothing().when(testedClass).methodToBeSkipped();
    
        testedClass.methodToBeTested();
        // Assertions etc.
    }