java PowerMockito 忽略方法调用

java PowerMockito ignore method call

在单元测试中,如何忽略对方法的调用,如下所示?

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

您不需要 for this. You could simply spy您要测试的对象并模拟您要跳过的方法:

@Test
public void testMethodToBeTested() {
    TestedClass testedClass = Mockito.spy(new TestedClass());
    Mockito.doNothing().when(testedClass).methodToBeSkipped();

    testedClass.methodToBeTested();
    // Assertions etc.
}