2017-06-27 30 views
0

我正在使用powermock。 我正面临着以下情况的问题。面向功率Mockito的问题私有空洞方法测试案例

UPDATE:

我的问题是不同的。 在另一个链接中给出的例子,私有方法返回一些值。 在我的情况下,这两个方法都返回Void。对于myPublicMethod用于编写测试用例

class ClassForWhichTestCasesIsPrepared { 

    private void myPrivateMethod(String param1, MyBean param2) { 
     //Some Code Here to save data 
    } 

    public void myPublicMethod() { 
     //Some Code Here to find the require paramters to pass to below method 

     myPrivateMethod(String param1, MyBean param2); 
    } 

} 

面临的问题嘲笑在同一类的私有方法。

我想嘲笑myPrivateMethod方法,因为它不应该被调用,但myPublicMethod应该覆盖测试用例。 这两种方法都是无效的。

我不能改变这个设计,我只需要完成并覆盖相同的测试用例。

+0

的可能的复制[如何嘲笑使用PowerMock测试私有方法?(https://stackoverflow.com/questions/7803944/how-to-mock-private-method-for-testing-using- powermock) –

回答

0

您可以使用PowerMock提供的间谍方法。

例如

ClassForWhichTestCasesIsPrepared spy = PowerMockito.spy(new ClassForWhichTestCasesIsPrepared()); 

    when(spy, method(ClassForWhichTestCasesIsPrepared .class, "myPrivateMethod", 
    String.class, MyBean.class)).withArguments(anyString(), anyObject()) 
    .thenReturn(true);// this return can be made to do nothing as well if you want 

spy.myPublicMethod();