Upgrading Lost to Robolectric 3.2
Lost relies heavily on the Android framework location classes especially
LocationManager. It also starts a background
Service and maintains a connection using
When dealing with functionality such services as location updates it would be technically challenging and possibly not a great use of time to write an abstraction layer for the Android framework. Therefore we use Robolectric to facilitate testing of this library.
The following stats were compiled in decidedly unscientific fashion running a handful of builds before and after the upgrade. The results however were rather consistent after the initial build.
Currently the project has 334 unit tests most written using Robolectric and some pure JUnit.
First I ran the tests in Android Studio using a JUnit run configuration for all tests in package similar to what you would do during development. Second I ran the tests on the Command Line locally using
./gradlew clean testDebug which is similar to what you might do in a CI environment.
Android Studio ~17 seconds
Command Line ~22 seconds
Android Studio ~23 seconds
Command Line ~29 seconds
- All instances of the deprecated class
RobolectricGradleTestRunnerneeded to be replaced with
RobolectricTestRunnersince the prior was completely removed in version 3.2.
ShadowLocationis now deprecated but we did not really depend on this implementation preferring to use our own mock
Locationobjects with Mockito.
- A fix I had submitted to resolve registration of duplicate listeners with the
ShadowLocationManagermade it into this release which is great. Unfortunately we still rely on a local copy of the shadow location manager in Lost because the method
requestSingleUpdate(...)still has no shadow implementation. Writing a working shadow for this method and contributing it has been on my TODO list for a while now!
- Finally we had one failing test after the upgrade. The original version of this test used two
PendingIntents that wrapped the same
Intentto start a
TestServiceto register for two sets of location updates. Due to a fix in the equals method for
ShadowPendingIntentthe location updates were correctly only registered once so the solution was to use a different
Intentfor a different
Servicewhen requesting the second set of location updates.
All in all the upgrade was rather painless. Testing this type of library project that is tightly tied to the Android framework would not be possible without Robolectric so for that I am thankful.
If you currently have a project using Robolectric 3.1.x I would encourage you to upgrade. If you do let me know how it goes in the comments.