Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"RobolectricTestRunner.class" not recognized in Android Studio

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
like image 708
Roberto Pinheiro Avatar asked Nov 14 '17 11:11

Roberto Pinheiro


People also ask

What is robolectric in Android Studio?

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.

Should I use robolectric or mock objects for Android unit tests?

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.

What is robolectric testing?

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.

How do I run robolectric tests on the JVM?

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.


1 Answers

I solved the issue just by putting the test class inside the src \ test folder instead of putting it in thesrc \ androidTest folder

like image 81
Roberto Pinheiro Avatar answered Sep 28 '22 04:09

Roberto Pinheiro