Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric does not support API level 28

Although this question has been answered for previous versions in other threads, none of the answers seems to work for me with api 28 right now so..

All Robolectric tests worked fine when on api 27. Now when my app api target is 28 they all fail.

I have

@Config(constants = BuildConfig::class, sdk = [Build.VERSION_CODES.P])

on my test class.

Diving into Robolectrics internal SdkConfig.java it looks like they add support for api 28:

addSdk(Build.VERSION_CODES.P, "P", "4651975", "P");

But when evaluating that line of code in the debugger, Build.VERSION_CODES.P evaluates to 10000. Not sure what's going on there.

I'm running Robolectric 3.8, and also tried with the 4.0 alpha version with no luck.

What am I missing?

Edit: For now I'm just running on the latest (what I can tell) supported api version, by annotating the test class(es) with @Config(sdk = [Build.VERSION_CODES.O_MR1]). This will give you Android 8.1.0 (api 27).

like image 391
Algar Avatar asked Jun 15 '18 06:06

Algar


People also ask

Is Robolectric deprecated?

Robolectric is intended to be fully compatible with Android's official testing libraries since version 4.0. As such we encourage you to try these new APIs and provide feedback. At some point the Robolectric equivalents will be deprecated and removed.

Where to put Robolectric properties?

properties File. To configure all Robolectric tests within a package or group of packages, create a file named robolectric. properties in the appropriate package. Generally, this file would be placed within the appropriate package directory under src/test/resources in your project tree.

What is Robolectric?

Robolectric is a framework that brings fast and reliable unit tests to Android. Tests run inside the JVM on your workstation in seconds. With Robolectric you can write tests like this: @RunWith(RobolectricTestRunner.


2 Answers

To complete Algar's answer, you can bypass temporarily this error (meanwhile Robolectric fix this issue) by annotating your test class like that :

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [Build.VERSION_CODES.O_MR1])
class YourUnitTests {

...

}

It will force Robolectric using Android API 27.

like image 85
Phil Avatar answered Sep 21 '22 09:09

Phil


Second edit: This has graduated from snapshot and is now available with

testImplementation 'org.robolectric:robolectric:4.0-beta-1'

i.e. you no longer need the maven snapshot line.


Edit: This is now fixed and available in the snapshot build. In your gradle build file (app level) add

repositories {
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}

and

dependencies {
    testImplementation 'org.robolectric:robolectric:4.0-alpha-3-SNAPSHOT'
}

Now that this issue is updated with info from a contributor it seems like there will be a new 4.0 alpha release that fixes the issue within this week.

like image 30
Algar Avatar answered Sep 17 '22 09:09

Algar