Howdy all. I’ve started doing UI test in compose. ...
# compose
k
Howdy all. I’ve started doing UI test in compose. I’ve followed https://developer.android.com/training/dependency-injection/hilt-testing and set hiltRule, composeRule, added HiltTestRunner as well as required dependencies. And I’m trying to figure out how do I provide viewModel into the test it self.
Copy code
@HiltAndroidTest
class CreateNoteScreenKtTest {

    @get:Rule(order = 1)
    val hiltRule = HiltAndroidRule(this)

    @get:Rule(order = 2)
    val composeTestRule = createAndroidComposeRule<MainActivity>()

    @Before
    fun setup() {
        hiltRule.inject()
    }

    @Test
    fun validation_message_is_removed_when_performed_input() {
        var navBackClicked = false
        var toolbarTitle = ""

        composeTestRule.setContent {
            toolbarTitle = stringResource(id = R.string.create_note_title)
            CreateNoteScreen(
                viewModel = composeTestRule.activity.viewModels<CreateNoteViewModel>().value,
                navigateBack = {
                    navBackClicked = true
                }
            )
        }
}
This results with
java.lang.RuntimeException: Cannot create an instance of class com.todo.ui.createnote.CreateNoteViewModel
Caused by: java.lang.NoSuchMethodException: com.todo.ui.createnote.CreateNoteViewModel.<init> []
Same error if I use
hiltViewModel
Any suggestions/materials?