"RobolectricTestRunner.class" not recognized in Android Studio
Asked Answered
G

6

24

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
Grossularite answered 14/11, 2017 at 11:24 Comment(3)
can you try with testImplementation instead of testCompile. It's depracated now with gradle version 4Autogenous
@Autogenous the error is the same with androidTestImplementation and not recognizing with testImplementation. I use the: androidTestImplementation 'com.android.support.test:runner:1.0.1' and it works correctly on the same project, just the roboletric that is not working.Grossularite
Robolectric is always test and not androidTest dependencyWoodring
G
47

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

Grossularite answered 21/11, 2017 at 17:52 Comment(6)
dude... you save lifeAzotobacter
@JacktheRipper LOL, glad to have helped!!Grossularite
Very nice. Thanks a lot. But why does that happen? Is there a way to configure it to accept src \ androidTest?Vareck
Nice!! It helps!!Paraffinic
It took me 1 hour to solve the issue until I found your answer, thank you.Anubis
Since this helped me as well - you can get it to wotk in androidTest by using androidTestImplementation instead of testImplementation in gradle.Miniature
C
13

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.

Commoner answered 16/5, 2020 at 14:19 Comment(2)
I just added both, androidTestImplementation and testImplementationHomologous
This is the right answer I must sayLuca
U
10

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)
Unpleasantness answered 15/2, 2021 at 20:52 Comment(0)
A
7

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

Autogenous answered 14/11, 2017 at 13:31 Comment(1)
I followed the official documentation, but the error continued ... Any other suggestions? Some more dependence, I don't know ...Grossularite
C
2

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

Comportment answered 8/10, 2023 at 5:32 Comment(0)
T
0

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

Taneshatang answered 4/8, 2023 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.