user6387287
user6387287

Reputation:

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.
}

Upvotes: 1

Views: 2883

Answers (1)

Mureinik
Mureinik

Reputation: 310993

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.
}

Upvotes: 6

Related Questions