在此处检测到未完成的存根

时间:2020-06-29 19:41:34

标签: java unit-testing mocking mockito powermockito

我有一个类似如下的课程:

public class ClassOne {

    public void function1(InputStream input, OutputStream output, Context context) {
    .....
    function2(List, String, String);
    } 
    
    private void function2(List, String, String){...}
}

我正在尝试为此类编写单元测试,如下所示:

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassOne.class)
public class ClassOneTest {
    private ClassOne classVar;
    private ClassOne classSpy;
    
    @Before
    public void setup() {
        classVar = new ClassOne();
        classSpy = new ClassOne(classVar);
    }
   
    @Test
    public void testFunction1() {
        ....
        PowerMokito.doNothing().when(classSpy, "function2", List, string, string);
    }

}

在上面的单元测试中,我收到此错误:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at org.powermock.core.classloader.ClassloaderWrapper.runWithClassClassLoader(ClassloaderWrapper.java:51)

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();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, which is not supported
 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed

我查看了几篇文章,但到目前为止没有任何帮助。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您没有致电spy(),这会导致问题

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassOne.class)
public class ClassOneTest {

    private ClassOne classVar;
    private ClassOne classSpy;
    
    @Before
    public void setup() {
        classVar = new ClassOne();
        classSpy = spy(new ClassOne(classVar));
    }

    @Test
    public void testFunction1() {
        // Arrange
        PowerMokito.doNothing().when(classSpy, "function2", List, string, string);

        // Act
        ....
    }
}