私有方法的doNothing的PowerMockito execption

时间:2017-05-02 10:45:10

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

我有一个类有两个私有方法我正在为一个方法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..

2 个答案:

答案 0 :(得分:0)

虽然您正在模拟的方法返回一个值,但是当您模拟类时我没有看到您返回值,powermockito框架在模拟时期望返回值,但是我还没有看到您发送模拟值,请尝试在类中返回模拟值。

答案 1 :(得分:0)

doNothing的实现是错误的。

Mockito.doNothing()时(mockedObject)。方法();