【问题标题】:Unfinished stubbing detected here此处检测到未完成的存根
【发布时间】:2020-06-29 19:41:34
【问题描述】:

我有一个像下面这样的课程:

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.您使用代码new ClassOne(classVar)中未显示的构造函数,您可以以某种方式将其用作模拟(没有NotAMockException)。 2. 变量需要专有名称。 3. PowerMokito 是错字吧?

标签: java unit-testing mocking mockito powermockito


【解决方案1】:

你没有打电话给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
        ....
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-07
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    相关资源
    最近更新 更多