AdrienM
AdrienM

Reputation: 75

Null pointer exception when stubbing

Good afternoon, I have a nullpointer exception when stubbing

package com.micheladrien.android.fresquerappel

import android.app.Application
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.micheladrien.fresquerappel.R
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock    
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.junit.MockitoJUnitRunner


@RunWith(MockitoJUnitRunner::class)
class MainViewModelTest {

  @Rule
  @JvmField
  var instantTaskExecutorRule = InstantTaskExecutorRule()    

  @Mock
  val applicationMock: Application = mock(Application::class.java)
 
  @Before
  fun setUpTaskDetailViewModel() {
        `when`(applicationMock.getString(R.string.collage_climat)).thenReturn("Climat")
    }
    }

edit : I need to stub the function when(applicationMock.getString(R.string.collage_climat)).thenReturn("Climat") because my viewmodel will get strings from context.

According to this blog post : https://codepills.com/2018/05/10/3-basic-mistakes-for-nullpointerexception-when-mock/ I should replace when thenreturn by when then (answer) which if it's true, why ?

I have already Tested : Changing R.id value to a brut number. => Same error Mocking the file inside the before and using lateinit for the declaration at @Mock => same error

Unlike previous question thread Mockito - NullpointerException when stubbing Method I am directly stubbing the method. Not stubbing the method of the object of another method.

Any help would be greatly appreciated.

Edit : The VM I aim to test :

class MainViewModel(application: Application): AndroidViewModel(application), WaitingViewModel{

    private val _name = MutableLiveData<String>().apply {
        value = application.getString(R.string.collage_climat)
    }

    val name : LiveData<String> = _name

    override fun notifyNewCollage(collage_name: String) {
        _name.value = collage_name
    }

}

Here is the gradle if you want to check the Mockito version :

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.3'
def navigation_version = '2.3.1'
implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "androidx.navigation:navigation-fragment:$navigation_version"
implementation "androidx.navigation:navigation-ui:$navigation_version"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
implementation 'il.co.theblitz:observablecollections:1.4.2'
def espressocore_version = '3.3.0'
androidTestImplementation "androidx.test.espresso:espresso-core:$espressocore_version"
androidTestImplementation "androidx.test.espresso:espresso-core:$espressocore_version"
androidTestImplementation "android.arch.core:core-testing:$lifecycle_version"
def mockito_version = '3.5.5' // For local unit tests on your development machine (also tested on 3.3.3)
testImplementation "org.mockito:mockito-core:$mockito_version" // For instrumentation tests on Android devices and emulators
androidTestImplementation "org.mockito:mockito-android:$mockito_version"
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'

}

Upvotes: 0

Views: 874

Answers (1)

AdrienM
AdrienM

Reputation: 75

Mockito advices against Mocking classes you don't own. So an Application mock is a bad idea. Junit can give you the application context needed : https://developer.android.com/training/testing/junit-runner

For other info about stubbing fail, Mockito fails on stubbing : it tries to execute the function that should be stubbed

Upvotes: 0

Related Questions