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?
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");
}
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"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With