1
votes

I have the following class that I am trying to test. The method I am having problems with is the showScollerView as I am trying to stub/mock the behaviour and then verify the behaviour in the test.

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
}

This is the test class

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

I get the error on this line:

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

This is the actual error message:

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

1

1 Answers

2
votes

The line you are getting an error should be:

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

just like the example from the 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();