【问题标题】:Can't use Mockito when and thenReturn不能使用 Mockito when and thenReturn
【发布时间】:2019-12-24 14:47:59
【问题描述】:

我正在尝试编写 mockito when 方法,以便在调用方法时可以定义返回的数据。 java方法:

 when(myRepository.insertNote(any(Note.class))).thenReturn(returnedData);

kotlin 中,我正在尝试编写相同的内容,但它没有显示在 when() 之后编写 thenReturn() 的选项.我正在尝试:

 when(mainRepository.fetchApiresultFromClient(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(),ArgumentMatchers.anyString(), ArgumentMatchers.anyInt()) )
        .thenReturn(returnedData)

我该如何解决?

【问题讨论】:

    标签: unit-testing kotlin mockito


    【解决方案1】:

    使用 doReturn 然后 when 作为 mockito 最佳实践:

    doReturn(returnedData).when(mainRepository)
    .fetchApiresultFromClient(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(),ArgumentMatchers.anyString(), 
        ArgumentMatchers.anyInt());
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    相关资源
    最近更新 更多