currently trying to test the interactions with ViewBinding classes in Unit Tests
"Invalid Input" should {
"disable the LoginButton" {
val viewBinding: FrLoginBinding = mockk()
InvalidInputViewStateBinder.bind(InvalidInput, viewBinding)
verify { viewBinding.loginButton.isEnabled = false }
}
}
something like this is what i had in mind. The Views in the ViewBinding are public final Properties and cannot easily be mocked. At least i'm unable to. Passing a View
mock to create the ViewBinding also doesn't work, as i'd have to mock findViewById
for it.
Has anyone tried this out and got it to work?
I came across this same issue. Here is how I resolved it
@RunWith(PowerMockRunner::class)
@PrepareForTest(MyLayoutBinding::class)
class MyTestClass {
@Mock
lateinit var mMockViewBinding: MyLayoutBinding
@Mock
lateinit var mMockView: View
@Mock
lateinit var mMockTitleTv: TextView
@Mock
lateinit var mMockRootView: ConstraintLayout
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
PowerMockito.mockStatic(MyLayoutBinding::class.java)
whenever(MyLayoutBinding.bind(mMockView)).thenReturn(mMockViewBinding)
// Use Whitebox for each view component in the layout.
Whitebox.setInternalState(mMockBinding, "title", mMockTitleTv)
// Because 'getRoot' is part of the ViewBinding interface, we just mock the method.
whenever(mMockBinding.root).thenReturn(mMockRootView)
}
}
Use Whitebox to set the properties (i.e. the views by id) and mock the getRoot() interface method to set the root to your mocked root view.
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