Does Robolectric require Java 9?

The_Martian picture The_Martian · Jun 30, 2019 · Viewed 26.3k times · Source

All the tests are passing, but I get the below warning. Robolectric is telling me that Java 9 is required. I am using the latest version of Robolectric.

[Robolectric] WARN: Android SDK 10000 requires Java 9 (have Java 8). Tests won't be run on SDK 10000 unless explicitly requested.
[Robolectric] com.example.testcaseWithRobolectric.MainActivityTest.testAllElements: sdk=28; resources=BINARY
Called loadFromPath(/system/framework/framework-res.apk, true); mode=binary sdk=28

Process finished with exit code 0

This is my Gradle:

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
        implementation project(path: ':domain_layer')
        testImplementation "org.robolectric:robolectric:4.3"
    }

defaultConfig {
        applicationId "com.example.testcaseWithRobolectric"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

Answer

chikwapuro picture chikwapuro · Jul 10, 2019

on your test class, you need to annotate with @Config with an array of sdk as a parameter.

@Config(sdk = {Build.VERSION_CODES.O_MR1})
class SampleTest {}

for Kotlin

@Config(sdk = [Build.VERSION_CODES.O_MR1])
class SampleTest {}

Your tests should run.