Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

412
Visualizações
Kotlin + Mockito + NullPointerException thrown when stubbing

Today I stumbled across a situation which I do not understand, possibly because of lack of insight into how mockito and mockito-kotlin work internally.

Given the code below, from my Kotlin beginner perspective, I have two pretty similar interface-methods. One returns Boolean, one String. Both are suspend functions in my example as in my real world situation my functions are too.

    class ITestInterface {
        suspend fun returnBoolean(): Boolean {
            return true
        }

        suspend fun returnSomeString() :  String {
            return "String"
        }
    }


    @Test
    fun demoTest() {
        val implMock = mock<ITestInterface> {
            on {
                runBlocking {
                    returnSomeString()
                }
            } doReturn "Hello"

            on {
                runBlocking {
                    returnBoolean()
                }
            } doReturn false
        }
    }

My observation is, when I run the test, like depicted above, I get the following error Message

com.nhaarman.mockitokotlin2.MockitoKotlinException: NullPointerException thrown when stubbing.
This may be due to two reasons:
    - The method you're trying to stub threw an NPE: look at the stack trace below;
    - You're trying to stub a generic method: try `onGeneric` instead.

    at com.nhaarman.mockitokotlin2.KStubbing.on(KStubbing.kt:72)
    at com.rewedigital.fulfillment.picking.components.substitute.DemoTest.demoTest(DemoTest.kt:30)

[...]

Experiments showed that

  • the behavior is only shown by the Boolean returning function, not by returnSomeString()
  • removing the suspend keyword fro the returnBoolean function makes the error go away
  • using onGeneric as suggested in the error message also makes the error go away

Could anybody explain why this is happening? To what extend do we have to do with generics here? And what would be the correct way of solving the issue in our real application? Having a bunch of on {} and some onGeneric {} ?

Thanks for your help!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You should use the onBlocking method to properly mock the suspend function

Please try the following code:

    @Test
    fun demoTest() {
        val implMock = mock<ITestInterface> {
            onBlocking {
                returnSomeString()
            } doReturn "Hello"

            onBlocking {
                returnBoolean()
            } doReturn false
        }

        runBlocking {
            // Execute your code here
            assertThat(implMock.returnSomeString()).isEqualTo("Hello")
            assertThat(implMock.returnBoolean()).isEqualTo(false)
        }
    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda