【问题标题】:PowerMockito execption for doNothing for a private methodPowerMockito 对私有方法无所事事的异常
【发布时间】:2017-09-29 20:43:44
【问题描述】:

我有一个有两个私有方法的类,我正在为一个方法 test() 编写 JUnit,并希望另一个方法 test1 什么都不做。就像这样..我要调用的方法是 test(),当它被称为 test1() 时我想要它什么都不做

class foo{

private void test1() {

//some code;
}

private int test( Element e){

test1(element e);

return 0;
}
}

我的测试课是这样的……

@RunWith(PowerMockRunner.class)
@PrepareForTest(Foo.class)

public class FooTest {

private Foo foo= new Foo;

public void testTest() {

element e;

Foo testOb= PowerMockito.spy(foo);

PowerMockito.doNothing().when(testOb, "test1", Matchers.any(Element.class));

 PowerMockito.doCallRealMethod().when(testOb, "test", e);

}

现在我面临的问题是它显示异常

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java)
E.g. thenReturn() may be missing.

Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();

now if i remove the "PowerMockito.doNothing().when(testOb, "test1", Matchers.any(Element.class));" line then it works fine but adding this produces the exception can anyone please help me..

【问题讨论】:

    标签: java unit-testing exception powermockito private-methods


    【解决方案1】:

    虽然你正在模拟的方法得到一个返回值,但我没有看到你在模拟类时返回一个值,powermockito 框架在模拟时期待一个返回值,但是我没有看到你发送一个模拟值,试试在类中返回模拟值。

    【讨论】:

    • 我正在模拟的返回值的方法将返回一个值,因为尽管我使用 power mock 返回了任何东西,但真正的方法将在那里被调用。第二个方法的问题不会返回任何东西,并且 doNothing() 行在那里抛出异常。没有那行它运行得很好。但我也必须模拟那个方法......
    【解决方案2】:

    doNothing 的实现是错误的。

    Mockito.doNothing().when(mockedObject).Method();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 2015-03-23
      • 2014-09-21
      • 1970-01-01
      • 2018-11-29
      相关资源
      最近更新 更多