Why can't I import AndroidJUnit4 and ActivityTestRule into my unit test class?
Asked Answered
V

10

87

I'm having trouble importing some of the Android UI testing framework classes - I just can't figure out what is going wrong!

This is my class:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ExampleUnitTest {

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);

@Test
public void listGoesOverTheFold() {
    onView(withText("Hello world!")).check(matches(isDisplayed()));
  }
}

But for some reason I get the errors 'cannot find symbol ActivityTestRule' and 'cannot find symbol AndroidJUnit4'. I've tried to import them but they cannot be found.

The dependencies in build.gradle are set to:

compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
androidTestCompile 'com.android.support:support-annotations:23.4.0'

androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'

So I think I have all the dependencies setup - I've been trying many things but with no luck.

Anyone have any ideas?

Vanda answered 14/6, 2016 at 20:5 Comment(3)
What directory is your test class in - test/java or androidTest/java?Cupronickel
It's under test/javaVanda
What if my test is in androidTest/java, and I'm having this problem?Woolson
C
67

There are two different types of tests you can set up in Android

Unit Tests

  • These run directly on the JVM and do not have access to the Android framework classes.
  • They are kept in the test/java package
  • Dependencies need to added in the build.gradle file with the command testCompile
  • You generally use Mockito, Robolectric & JUnit for these tests

Instrumentation Tests

  • These run on an Android emulator and have full access to all the Android classes
  • They are kept in the androidTest/java package
  • Dependencies need to be added to build.gradle with androidTestCompile
  • You generally use Espresso and JUnit for these tests

From what I can tell you are trying to write instrumentation tests with Espresso but have your test in the test/java package which is for unit tests. In that case you need to move your test class to the androidTest/java package.

Cupronickel answered 15/6, 2016 at 15:54 Comment(2)
Thanks, that's solved it! I didn't realise there was a difference between test/java and androidTest/javaVanda
Make sure you also TestImplement 'com.android.support.test:rules:1.0.2'Quadrivium
S
140

Add these in the newer version:

androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
Shrieval answered 21/5, 2018 at 6:27 Comment(0)
C
67

There are two different types of tests you can set up in Android

Unit Tests

  • These run directly on the JVM and do not have access to the Android framework classes.
  • They are kept in the test/java package
  • Dependencies need to added in the build.gradle file with the command testCompile
  • You generally use Mockito, Robolectric & JUnit for these tests

Instrumentation Tests

  • These run on an Android emulator and have full access to all the Android classes
  • They are kept in the androidTest/java package
  • Dependencies need to be added to build.gradle with androidTestCompile
  • You generally use Espresso and JUnit for these tests

From what I can tell you are trying to write instrumentation tests with Espresso but have your test in the test/java package which is for unit tests. In that case you need to move your test class to the androidTest/java package.

Cupronickel answered 15/6, 2016 at 15:54 Comment(2)
Thanks, that's solved it! I didn't realise there was a difference between test/java and androidTest/javaVanda
Make sure you also TestImplement 'com.android.support.test:rules:1.0.2'Quadrivium
G
67

If you migrated to AndroidX, use this:

androidTestImplementation 'androidx.test:rules:1.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
Gust answered 18/12, 2018 at 14:33 Comment(0)
F
37

Adding:

androidTestImplementation 'com.android.support.test:rules:1.0.2'

solves problem, but don't forget to sync the project with gradle files. Only then the changes will take effect.

Foresee answered 28/7, 2018 at 9:36 Comment(1)
note: com.android.support packages are deprecated now. Developers should use androidx equivalent packagesPlayground
E
24

need this add dependencies

 testCompile 'com.android.support.test:rules:0.5'
 testCompile 'com.android.support.test:runner:0.5'
Entoil answered 5/2, 2017 at 16:33 Comment(2)
'com.android.support.test:rules:0.5' and 'com.android.support.test:runner:0.5' are for androidTest, not test. so it should be androidTestCompile 'com.android.support.test:rules:0.5' androidTestCompile 'com.android.support.test:runner:0.5'Rhyne
androidTestImplementation form Android Studio 3.x onListless
B
14

From androidX use

androidTestImplementation 'androidx.test:rules:1.1.1'

androidTestImplementation 'androidx.test:runner:1.1.1'

In your dependencies section in your app level build.gradle file

for eg :

 dependencies {

     androidTestImplementation 'androidx.test:rules:1.1.1'
     androidTestImplementation 'androidx.test:runner:1.1.1'
 }

Then import

import androidx.test.rule.ActivityTestRule;

Bellyache answered 2/5, 2020 at 5:39 Comment(0)
C
11

Add dependency.

androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test:runner:0.5'
Chilon answered 8/5, 2018 at 10:31 Comment(0)
P
8

For writing UI tests((tests that run on Android Device/Emulator)) in Android, make sure that

  1. The Test classes are in androidTest package rather that test package.

  2. Make sure you make the following dependencies in build.gradle

androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
testImplementation 'junit:junit:4.13'

For Unit Testing(tests that run on JVM) make sure

1.Test classes are in test package

2.Make sure you make the following dependencies in build.gradle

testImplementation 'junit:junit:4.13'
testImplementation 'org.mockito:mockito-core:2.23.0'
Pedo answered 27/4, 2020 at 4:22 Comment(1)
Adding the androidTestImplementation and testImplementation did the trick for me. Thanks!Dynamometer
I
0

Using ActivityScenarioRule worked for me.

    @Rule
public ActivityScenarioRule<MainActivity> mActivityRule = new ActivityScenarioRule(MainActivity.class);
Ipsambul answered 17/8, 2021 at 12:17 Comment(0)
S
0

you need to add these dependencies

    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
Stamey answered 17/7, 2022 at 10:33 Comment(1)
The similar answer is already exists: https://mcmap.net/q/237331/-why-can-39-t-i-import-androidjunit4-and-activitytestrule-into-my-unit-test-classLionellionello

© 2022 - 2024 — McMap. All rights reserved.