@Config constants parameter could not be found in Robolectric

4.5k views Asked by At

I am trying to write a Robolectric test. I was following few tutorials where they seem to be using

@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class)

to setup the test, but in my case The parameter constants does not seem to resolve.

enter image description here

My Robolectric dependency looks like this:

testImplementation "org.robolectric:robolectric:4.0.2"
2

There are 2 answers

1
shizhen On

Robolectric is for unit test, not for androidTest, so please confirm that your test case is under src/test, NOT under src/androidTest.

0
Guerneen4 On

constants parameter is now deprecated see the doc :

constants
Deprecated. 
If you are using at least Android Studio 3.0 alpha 5 please migrate to the 
preferred way to configure builds for Gradle with AGP3.0 
http://robolectric.org/getting-started/

The proper way to set up Robolectric per the documentation is :

android {
  testOptions {
    unitTests {
      includeAndroidResources = true
    }
  }
}

dependencies {
  testImplementation 'org.robolectric:robolectric:4.1'
}