1

Here's the crash log

java.lang.NullPointerException: Null reference used for synchronization (monitor-enter)
    at androidx.lifecycle.ViewModel.setTagIfAbsent(ViewModel.java:156)
    at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:70)
    at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:84)
    at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:109)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)

Here's my activity test code

@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class LoginActivityTest {

    @BindValue
    @JvmField
    val viewModel = mockk<LoginViewModel>(relaxed = true)

    @get:Rule
    var hiltRule = HiltAndroidRule(this)

    private lateinit var scenario: ActivityScenario<LoginActivity>
    private val robot: LoginRobot = LoginRobot()
    private val loginLD = MutableLiveData<SimpleResource>()

    fun launch() {
        scenario = ActivityScenario.launch(LoginActivity::class.java)
    }

    @Before
    fun setup() {
        Intents.init()
        hiltRule.inject()
        every { viewModel.getLoginLD() } returns loginLD
        IdlingRegistry.getInstance().register(EspressoIdlingResource.idlingResource)
    }

    @After
    fun teardown() {
        IdlingRegistry.getInstance().unregister(EspressoIdlingResource.idlingResource)
        Intents.release()
    }

    @Test
    fun should_open_login_activity() {
        robot.launch(this)
    }

    @Test
    fun should_open_main_activity_on_submitting_correct_username_and_password() {
        // Two things pending,
        // 1. mocking view model,
        val username = "Sanket"
        val password = "Sanket"

        robot.launch(this)
            .checkIfUsernameEditTextIsVisible()
            .checkIfPasswordEditTextIsVisible()
            .checkIfLoginBtnIsVisible()
            .enterUserName(username)
            .enterPassword(password)
            .clickLoginBtn()
            .sendMockSuccessResponse(loginLD)
            .checkIfMainActivityIsOpen()
    }

}

The way I initialize my viewmodel in activity

private val viewModel: LoginViewModel by lazy { ViewModelProvider(this).get(LoginViewModel::class.java) }

I tried by viewModels delegation as well, the error stays the same

My dependencies

implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'

    implementation "androidx.test.espresso:espresso-idling-resource:3.4.0"

    implementation "androidx.activity:activity-ktx:1.4.0"

    //Hilt
    implementation "com.google.dagger:hilt-android:2.38.1"
    kapt "com.google.dagger:hilt-compiler:2.38.1"
    kaptAndroidTest "com.google.dagger:hilt-compiler:2.38.1"
    androidTestImplementation "com.google.dagger:hilt-android-testing:2.38.1"
    kaptAndroidTest "com.google.dagger:hilt-android-compiler:2.38.1"

    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.mockito:mockito-core:3.9.0'
    testImplementation 'androidx.arch.core:core-testing:2.1.0'
    testImplementation 'org.powermock:powermock-module-junit4:2.0.9'
    testImplementation "org.powermock:powermock-api-mockito2:2.0.9"
    testImplementation 'org.mockito.kotlin:mockito-kotlin:3.2.0'

    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.test.espresso:espresso-intents:3.4.0"
    androidTestImplementation "io.mockk:mockk-android:1.10.0"

I am not sure what the issue is? I had followed this for reference and it should've worked

Let me know if any additional information is needed. Here's the repo/branch where I am trying this out. Any help will be appreciated

sanket vetkoli
  • 826
  • 14
  • 18

0 Answers0