Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

665
Vistas
mockito-kotlin whenever arguments - Parameter specified as non-null is null

For testing I wrote a wrapper for ViewModelProviders.of(...).get(...) so that I can supply a mock:

class MyViewModelProvider {
    fun <T : ViewModel> get(scope: Fragment, type: Class<T>) : T {
        return ViewModelProviders.of(scope).get(type)
    }

    ...
}

And then in my test:

Dagger TestModule:

import com.nhaarman.mockito_kotlin.mock

...

fun provideMyViewModelProvider(): MyViewModelProvider {
        return mock()
}

And in my test class:

import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.anyOrNull
import com.nhaarman.mockito_kotlin.whenever
...
@Inject
lateinit var mockViewModelProvider: MyViewModelProvider
...
val mockViewModel = mock<MyViewModel>()

whenever(mockViewModelProvider.get(any<Fragment>(), any<Class<ViewModel>>())).thenReturn(mockViewModel)
...

However on the line with whenever I get the error:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter scope

I've tried a couple things:

  1. Swapping any<Fragment>(), any<Class<ViewModel>>()) with anyOrNull<Fragment>(), anyOrNull<Class<ViewModel>>() --> still get the same error

  2. Tried using concrete references whenever(mockViewModelProvider.get(myFragment, MyViewModel::class.java)).thenReturn(mockViewModel) --> Calls actual method instead of mock

Not sure what I'm doing wrong...

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Found the answer:

The mocked function needs to be marked open or use mock-maker-inline.

https://github.com/nhaarman/mockito-kotlin/wiki/Parameter-specified-as-non-null-is-null

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda