DEV Community

Aniket Kadam
Aniket Kadam

Posted on

Robolectric, doesn't really require Java 9

Sometimes you'd need Robolectric to test things, like JodaTime, if you didn't want to go to the effort of either DI replacing it or running on a real device everytime.

When you do so, if you just use whatever the default your manifest runs on, you could face issues with it saying:

[Robolectric] WARN: Android SDK 29 requires Java 9 (have Java 8). Tests won't be run on SDK 29 unless explicitly requested.

java.lang.UnsupportedOperationException: Failed to create a Robolectric sandbox: Android SDK 29 requires Java 9 (have Java 8)

Usually when your target SDK is set to the latest, which is what Robolectric will use by default.

The Fix

Instead of:

@RunWith(RobolectricTestRunner::class)
class EmotionJourneyToViewUseCaseTest {....}

Add:

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [Config.OLDEST_SDK])
class EmotionJourneyToViewUseCaseTest {....}

Sure, now you're only testing against the minSdk in your project, and this might not be adequate for you depending on what you're testing. Particularly if the test requires something in a later version of the SDK!
Please consider your individual test situation.
However, if all you need is JodaTime working, this works great!


If you want to hire me to develop for Android at a senior/team lead level and sort out tricky situations in such a way that the entire team speeds up, reach out on anikadamg att gmail!

Top comments (0)