5
votes

I constantly get kotlin.UninitializedPropertyAccessException: lateinit property xxx has not been initialized in my Mockito test. But the app works just fine. Note: I don't want to inject presenter into activity. Thanks in advance!

Here's my Activity:

class CreateAccountActivity : AppCompatActivity(), CreateAccountView {

private var presenter: CreateAccountPresenter? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_create_account)
    presenter = CreateAccountPresenter()
    ((application) as CariumApp).getDaggerComponent().inject(presenter!!)

    presenter?.attachView(this)
}

And here's my Presenter:

class CreateAccountPresenter {
private var view: CreateAccountView? = null

@Inject
lateinit var dataManager: DataManager

fun attachView(view: CreateAccountView) {
    this.view = view

    dataManager.getServiceDocuments(true, object : GetServiceDocumentsListener { 
       // ...
    })
}

Here's my DataManager:

interface DataManager {
    fun getServiceDocuments(latest: Boolean, listener: GetServiceDocumentsListener)
}

and AppDataManager:

Singleton
class AppDataManager @Inject constructor(context: Context) : DataManager {
// ...
}

and finally my test that's failing:

class CreateAccountPresenterTest {

val mockDataManager: DataManager = mock()

val mockCreateAccountView: CreateAccountView = mock()

private val createAccountPresenter = CreateAccountPresenter()

@Test
fun getServiceDocuments() {
    doAnswer {
        val args = it.arguments
        (args[1] as GetServiceDocumentsListener).onError()
        null
    }.`when`(mockDataManager).getServiceDocuments(Mockito.anyBoolean(), anyOrNull())

    createAccountPresenter.attachView(mockCreateAccountView)

    verify(mockCreateAccountView).hideLoadingDialog()
}
}

gradle file:

testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.22.0'
testImplementation "org.mockito:mockito-inline:2.22.0"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-RC1"
implementation 'com.google.dagger:dagger:2.16'
kapt 'com.google.dagger:dagger-compiler:2.16'

My module class:

@Module
open class MyModule(private var context: Context) {

@Provides
open fun provideContext(): Context {
    return context
}

@Provides
@Singleton
internal fun provideDataManager(appDataManager: AppDataManager): DataManager {
    return appDataManager
}
}

Actual error is kotlin.UninitializedPropertyAccessException: lateinit property dataManager has not been initialized

2

2 Answers

2
votes

You are not assigning your mock to the field. Assign it in your test method. Before calling attachView()

createAccountPresenter.dataManager = mockDataManager
0
votes

Where do you have DataManager @Provides method? Dagger recognizes @Inject constructor inside AppDataManager but cannot recognize it as interface. Create Module for Dagger that is abstract and uses @Binds

https://proandroiddev.com/dagger-2-annotations-binds-contributesandroidinjector-a09e6a57758f