android - 返回 void 返回类型的模拟 stub

标签 android kotlin mockito

我正在尝试测试以下类(class)。我遇到问题的方法是 showScollerView,因为我试图对行为进行 stub /模拟,然后在测试中验证行为。

class CustomScrollerView @JvmOverloads constructor(
        context: Context,
        attributeSet: AttributeSet? = null,
        styleAttributes: Int = 0)
    : ConstraintLayout(context, attributeSet, styleAttributes) {

    private var fragment: ConstraintLayout by Delegates.notNull()
    private var layoutResEnding: Int = 0
    private val transition = ChangeBounds()
    private val constraintSet = ConstraintSet()
    private var isShowing = false

    init {
        View.inflate(context, R.layout.overview_scroller_view, this)
        transition.interpolator = AccelerateInterpolator()
        transition.duration = 300
    }

    fun <L: ConstraintLayout> setView(view: L) {
        fragment = view
    }

    fun setLayoutResourceFinish(@LayoutRes id: Int) {
        layoutResEnding = id
    }

    fun showScrollerView() {
        constraintSet.clone(context, layoutResEnding)
        TransitionManager.beginDelayedTransition(fragment, transition)
        constraintSet.applyTo(fragment)
        isShowing = true
    }

    fun isScrollViewShowing() = isShowing
}

这是测试类

class CustomScrollerViewTest: RobolectricTest() {
    @Mock
    lateinit var constraintSet: ConstraintSet
    @Mock
    lateinit var constraintLayout: ConstraintLayout

    private var customScrollerView: CustomScrollerView by Delegates.notNull()

    @Before
    fun setup() {
        customScrollerView = CustomScrollerView(RuntimeEnvironment.application.baseContext)
    }

    @Test
    fun `test that CustomScrollerView is not null`() {
        assertThat(customScrollerView).isNotNull()
    }

    @Test
    fun `test that the scrollerView is shown`() {
        doNothing().`when`(constraintSet.clone(RuntimeEnvironment.application.baseContext, R.layout.fragment)) /* Error here */
        doNothing().`when`(constraintSet).applyTo(constraintLayout)

        customScrollerView.setLayoutResourceFinish(R.layout.fragment)
        customScrollerView.setView(constraintLayout)
        customScrollerView.showScrollerView()

        assertThat(customScrollerView.isScrollViewShowing()).isEqualTo(true)
        verify(constraintSet).applyTo(constraintLayout)
        verify(constraintSet).clone(RuntimeEnvironment.application.baseContext, R.layout.fragment)
    }
}

我在这一行得到了错误:

doNothing().when(constraintSet.clone(RuntimeEnvironment.application.baseContext, R.layout.fragment))

这是实际的错误信息:

Unfinished stubbing detected here: -> at com.nhaarman.mockito_kotlin.MockitoKt.doNothing(Mockito.kt:108)

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

最佳答案

您遇到错误的那一行应该是:

doNothing().`when`(constraintSet).clone(RuntimeEnvironment.application.baseContext, R.layout.fragment)

就像 javadoc here 中的示例一样:

List list = new LinkedList();
List spy = spy(list);

//let's make clear() do nothing
doNothing().when(spy).clear();

spy.add("one");

//clear() does nothing, so the list still contains "one"
spy.clear();

关于android - 返回 void 返回类型的模拟 stub ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51740142/

相关文章:

Android - 编译后 Kotlin 文件存在于 apk 中

c# - Kotlin 相当于 C# 事件

java - 单元测试时的 NPE

java - 尝试使用 mockito 抛出检查异常的问题

android - flutter DraggableScrollableSheet 与粘性标题

java - 分页库最初返回空列表

java - 当我按下按钮返回 android 键盘时,WebView 正在关闭

从播放列表中检索时的 Android 歌曲 ID 与我从媒体中检索时的不同

android - 类型不匹配 : inferred type is but was expected

java - Mockito 模拟类返回 Null