Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Unit Tests with SparseIntArray and others from android.util.*

The Android Studio recommends me to use SparseIntArray instead of Hashmap<Integer, Integer> from standard Java.

SparseIntArray map integers to integers. Unlike a normal array of integers, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Integers, both because it avoids auto-boxing keys and values and its data structure doesn't rely on an extra entry object for each mapping.

I think it really fits my code, but I cannot use any methods containing this class in Local Unit Tests. I always end with RuntimeException...

java.lang.RuntimeException: Method put in android.util.SparseIntArray not mocked. See http://g.co/androidstudio/not-mocked for details.
   at android.util.SparseIntArray.put(SparseIntArray.java)
   ...

Is there any way to import this method for local testing? I don't think it uses any Android's features. The unitTests.returnDefaultValues = true only returns default values for all calls. I know I can use Instrumented Unit Tests but it slows down the testing process.

like image 300
maresmar Avatar asked Aug 31 '26 01:08

maresmar


1 Answers

You should use Robolectric framework if you want to test class with platform dependencies: http://robolectric.org/

like image 145
Atetc Avatar answered Sep 01 '26 16:09

Atetc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!