首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >此处检测到未完成的存根

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

Stack Overflow用户
提问于 2020-06-30 03:41:35
回答 1查看 291关注 0票数 0

我有一个如下所示的类:

代码语言:javascript
复制
public class ClassOne {

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

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

代码语言:javascript
复制
@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);
    }

}

我在上面的单元测试中得到了这个错误:

代码语言:javascript
复制
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

我看了几篇帖子,但到目前为止都没有什么帮助。任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-30 04:53:50

您没有调用spy(),这导致了问题

代码语言:javascript
复制
@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
        ....
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62645571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档