Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock Kotlin's kotlinx.android.synthetic views in Android

I have a fragment written in Kotlin. I import layout views using

import kotlinx.android.synthetic.main.my_fragment_layout.

In one of my methods I am setting the TextView's text:

fun setViews() {
    myTextView.text = "Hello"
    // In Java I would have used:
    // (getView().findViewById(R.id.myTextView)).setText("Hello");
}

In my plain JVM unit test, I want to test this method using Mockito. For example, if the above method was written in java I could do:

public void setViewsTest() {
    // Mock dependencies
    View view = Mockito.mock(View.class);
    TextView myTextView = Mockito.mock(TextView.class);
    when(fragment.getView()).thenReturn(view);
    when(view.findViewById(R.id. myTextView)).thenReturn(myTextView);

    // Call method
    fragment.setViews();

   // Verify the test
   verify(myTextView).setText("Hello");
}

How can I do a similar implementation when using Kotlin's kotlinx.android.synthetic views?

like image 279
Ashish Jha Avatar asked Oct 26 '17 13:10

Ashish Jha


2 Answers

I think that Robolectric is a more proper tool for such type of tests. Using it you can have an easier way to test a code with Android dependencies on JVM.

For example, you test will look like something like this:

@Test
fun `should set hello`() {
    val fragment = YourFragment()

    fragment.setViews()

    assertEquals(fragment.myTextView.getText().toString(), "Hello");
}
like image 166
andrei_zaitcev Avatar answered Nov 15 '22 00:11

andrei_zaitcev


I've been looking for a solution for this problem too, and this is what I'm currently doing to mock views to be able to verify them.

This solution does not use kotlinx.android.synthetic since I haven't yet found a way to avoid the "Method not mocked" exception, instead I go with the Butterknife way.

In the activity or fragment:

@BindView(R.id.my_text_view)
internal lateinit var myTextView: TextView
// ...
fun setViews() {
    myTextView.text = "Hello"
}

In the test:

import com.nhaarman.mockito_kotlin.mock

class MyActivityTest {

    private val underTest = MyActivity()

    @Before
    fun setUp() {
        underTest.myTextView = mock()
    }

    @Test
    fun setViews() {
        underTest.setViews()

        verify(underTest.myTextView).text = "Hello"
    }
}
like image 34
Sebastian Avatar answered Nov 14 '22 23:11

Sebastian