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).
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With