Roberto Pinheiro
Roberto Pinheiro

Reputation: 1320

"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

Upvotes: 24

Views: 10595

Answers (6)

Zeke
Zeke

Reputation: 31

If your test file is written in kotlin then use @RunWith(RobolectricTestRunner::class)

Upvotes: 2

I have to move (downgrade) the version from 4.13.2 to 3.3.2

Upvotes: 0

mobibob
mobibob

Reputation: 8794

I followed most of the recommendations without any luck.
I then changed a dot (.) to double-colon (::)

was ...

@RunWith(RobolectricTestRunner.class)

changed to ...

@RunWith(RobolectricTestRunner::class)

Upvotes: 10

ManuQiao
ManuQiao

Reputation: 758

Usually there will be 2 kinds of tests 'androidTest' and 'test'.

If you add dependency like androidTestImplementation 'org.robolectric:robolectric:4.3.1', Robolectric package will only be imported in 'androidTest' classes.

If you add dependency like testImplementation 'org.robolectric:robolectric:4.3.1', they can only be imported in 'test' classes.

Upvotes: 15

Roberto Pinheiro
Roberto Pinheiro

Reputation: 1320

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

Upvotes: 48

Fakher
Fakher

Reputation: 2128

RobolectricGradleTestRunner is deprecated since version 3 of Robolectric. Just use @RunWith(RobolectricTestRunner.class) Take a look at official documentation.

Upvotes: 7

Related Questions