0

Estou fazendo testes unitários da minha ViewModel e possuo um metodo que salva o dado no Firebase gerando um id internamente com o auxilio da classe Base64. Na minha estrutura de classes eu chamo o metodo viewModel.saveService, que chamado o metodo keyHash responsavel por instanciar minha classe que faz a criptografia. nesse caso estou querendo testar o metodo viewModel.saveService() e estou tendo a seguinte Exception na linha onde possuo o código -> whenever(servicesViewModel.setKeyHash(services.name)).thenReturn(hash) Exception :

org.mockito.exceptions.misusing.NullInsteadOfMockException: 
Argument passed to when() is null!
Example of correct stubbing:
    doThrow(new RuntimeException()).when(mock).someMethod();
Also, if you use @Mock annotation don't miss initMocks()

Test:

@Test
fun `Should save Service`() = runBlockingTest{
    val services = getSomeService()
    val hash = "oa69AWa3dw6aqa"

    mockkStatic(Base64::class)
    doNothing().whenever(Base64.encodeToString(any(), anyInt()))
    whenever(servicesViewModel.setKeyHash(services.name)).thenReturn(hash)

    servicesViewModel.saveService(services.name, services.description!!, services.cost!!, services.duration!!)

    verify(repository).saveService(services)

}

fun getSomeService() = Services("someId",
    "someName",
    "someDescription",
    "someCost",
    "someDurarion")

ViewModel.kt

fun saveService(name: String?, description: String, cost: String, duration: String) = runBlocking (Dispatchers.IO){

    repository.saveService(Services(
        id = setKeyHash(name),
        name = name,
        description = description,
        cost = cost,
        duration = duration))
}

fun setKeyHash(s: String?): String{
    val hash = HashGenerator()
    return hash.encript(s!!)
}

HashClass

class HashGenerator {

fun encript(text: String): String {
    return Base64.encodeToString(text.toByteArray(),
        Base64.DEFAULT).replace("(\\n|\\r)".toRegex(),
        "")
}

}
2
  • ja tentou com powermockito? 18/09/2019 às 0:29
  • sim, segui alguns tutoriais e os mesmos continuam barrando na classe Base64. 18/09/2019 às 1:42

0

Você deve fazer log-in para responder a esta pergunta.

Pesquise outras perguntas com a tag .