java PowerMockito忽略方法调用

时间:2018-01-09 17:32:42

标签: java unit-testing mocking mockito powermockito

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

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

1 个答案:

答案 0 :(得分:4)

您不需要。您可以简单地spy您要测试的对象并模拟您想要跳过的方法:

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

    testedClass.methodToBeTested();
    // Assertions etc.
}
相关问题