【问题标题】:mocking stubs that return a void return type模拟返回 void 返回类型的存根
【发布时间】:2019-01-15 07:58:52
【问题描述】:

我正在尝试测试以下课程。我遇到问题的方法是showScollerView,因为我试图对行为进行存根/模拟,然后在测试中验证行为。

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))

这是实际的错误信息:

此处检测到未完成的存根: -> 在 com.nhaarman.mockito_kotlin.MockitoKt.doNothing(Mockito.kt:108)

例如thenReturn() 可能会丢失。 正确的存根示例: 当(模拟。isOk())。然后返回(真); when(mock.isOk()).thenThrow(异常); doThrow(exception).when(mock).someVoidMethod(); 提示: 1. 缺少 thenReturn() 2.您正在尝试存根不支持的最终方法 3:如果完成,您将在“thenReturn”指令之前存根另一个模拟的行为

【问题讨论】:

    标签: android kotlin mockito


    【解决方案1】:

    你得到错误的那一行应该是:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 2015-03-24
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      相关资源
      最近更新 更多