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

767
Visualizações
How to mock Build.VERSION.SDK_INT using mockk

How can I mock Build.VERSION.SDK_INT in mockk?

I've done the following:

@Test
fun testFoo(){
    mockkStatic(Build::class)
    mockkStatic(Build.VERSION::class)
    every {
        Build.VERSION.SDK_INT
    } answers { 22 }
}

I end up getting io.mockk.MockKException: Missing calls inside every { ... } block. once the code hits the every block.

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

0

You can build a wrapper around the Build config like this

object MyAppBuildConfig {
    fun getVersionSDKInt(): Int {
        return Build.VERSION.SDK_INT
    }
}

Then mock the MyAppBuildConfig with mockkObject and return your desire version number

    mockkObject(MyAppBuildConfig)
    every { MyAppBuildConfig.getVersionSDKInt() } returns 22
over 4 years ago · Santiago Trujillo Relatório

0

I ended up modifying Build.VERSION.SDK_INT using reflection. I added the following helper:

private fun setStaticFieldViaReflection(field: Field, value: Any) {
  field.isAccessible = true
  Field::class.java.getDeclaredField("modifiers").apply {
    isAccessible = true
    setInt(field, field.modifiers and Modifier.FINAL.inv())
  }
  field.set(null, value)
}

and then called it like this in my test:

setStaticFieldViaReflection(Build.VERSION::class.java.getField("SDK_INT"), 23)
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