4

I have found other similar questions but none of the answers were useful. In a method I have something similar to:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    // use API available only on Q
} else { ... }

When writing the unit test I would like to be able to check all the conditions.

I have found people saying that I should wrap the check Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q in an object that I can then mock.

If I do that Android Studio shows me a warning because I'm using an API that is for Q+

Other people say that I should use Mockk that allows you to mock static method. The problem is that Build.VERSION.SDK_INT does not call a method but just a field and I did not find the way to mock it.

Is there a way to do it?

PS I'm using Kotlin

1048576
  • 615
  • 9
  • 27
  • Is this Local unit test for JVM? – Style-7 Jul 14 '20 at 18:58
  • @Style-7 I don't know what you mean with `Local` unit test. I'm writing Junit5 unit tests. They are not instrumentation tests so I don't need to access the framework. – 1048576 Jul 15 '20 at 12:37
  • Are you run it by Android Studio or by device – Style-7 Jul 15 '20 at 15:52
  • Sorry @Style-7 but I still don't understand what you mean. I'm running the unit tests sometimes with Android Studio sometimes with gradle directly either on my laptop or on CI. I don't need to have a device to run them because they are just unit tests and not instrumentation tests. – 1048576 Jul 28 '20 at 15:33
  • any closure to this? – Harsh Vardhan Jul 14 '22 at 03:47
  • I didn't find anything @HarshVardhan – 1048576 Jul 18 '22 at 15:17
  • Does this answer your question? [How to mock Build.VERSION.SDK\_INT using mockk](https://stackoverflow.com/questions/54356739/how-to-mock-build-version-sdk-int-using-mockk) – Jadent Feb 02 '23 at 17:01

0 Answers0