Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

764
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!