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

437
Visualizações
Using mockk to match any varargs

I'm trying to mock an Android Context to return a string from a resource id. However I have trouble matching the stub to the call, I assume it is because of the varargs. However I am new to mockk so I might just miss something very easy.

I mock the context this way:

val context = mockk<Context>()
every { context.getString(any(), any()) } returns stringToReturn

But when calling getString on the object it throws the following exception:

io.mockk.MockKException: no answer found for: Context(#1).getString(2131689544, [])

If it is important, I call the function in the class under test similar to this. formatArgs may be empty but doesn't have to:

protected fun foo(stringResource: Int, vararg formatArgs: Any) {
    val s = context.getString(errorMessageStringResource, *formatArgs)

Any idea how I can fix this?

You can check the project and reproduce the exception here: Github Project

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

0

There is a related open issue in mockk v1.9: https://github.com/mockk/mockk/issues/224 (see referenced issues as well)

I tried several solutions but I ended up creating overloaded functions just for testing with mockk, eg.

class Context {
    // Renamed because of same JVM signature
    fun foo2(stringResource: Int, vararg formatArgs: Any) = foo(stringResource, formatArgs)

    // Function accepts 
    fun foo(stringResource: Int, formatArgs: args: Array<out Any>) = ...
}

then test the non-vararg foo() function with mockk.

I know it's an ugly workaround but if you find a better one please let me know :)

over 4 years ago · Santiago Trujillo Relatório

0

Version 1.9.1 introduces few additional matchers to match varargs.

https://mockk.io/#varargs

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