I'm trying to use Robolectric to run unit tests but the Android Studio is not recognizing the class on:
@RunWith(RobolectricTestRunner.class)
Details:
classpath 'com.android.tools.build:gradle:3.0.0'
And I'm importing dependency:
testImplementation "org.robolectric:robolectric:3.5.1"
And:
android {
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
It simply does not appear option to import the class. Should I add any more dependencies I'm forgetting?
Error:
error: package org.robolectric does not exist
error: package org.robolectric does not exist
error: package org.robolectric.shadows does not exist
error: cannot find symbol class RobolectricTestRunner
error: cannot find symbol variable Robolectric
error: cannot find symbol variable ShadowApplication
1 It provides a way to run our tests inside Android Studio, without launching an app on Device or Emulator. 2 Shadow Classes, rewritten android core libraries by Robolectric, are the real magic of Robolectric. It is a replica of the Android class with some exposed useful functions.
If your tests depend on objects in the Android framework, we recommend using Robolectric. For tests that depend on your own dependencies, use mock objects to emulate your dependencies' behavior. Unit tests run on an Android device or emulator. These tests have access to instrumentation information, such as the Context for the app under test.
Robolectric, it is a unit testing framework that allows Android applications to be tested on the JVM without an emulator 1 It provides a way to run our tests inside Android Studio, without launching an app on Device or Emulator. 2 Shadow Classes, rewritten android core libraries by Robolectric, are the real magic of Robolectric.
Your tests should be stored in the src/test directory. The class containing your Robolectric test must be annotate with the @RunWith (RobolectricGradleTestRunner.class) test runner. It must also use the @Config () to point to your BuildConfig.class class. The following shows an example test configured to run via Robolectric on the JVM.
I solved the issue just by putting the test class inside the src \ test
folder instead of putting it in thesrc \ androidTest
folder
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