Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android. @RunWith(AndroidJUnit4.class) - cannot resolve in package "androidTest"

Android Studio 2.2.3

Install Android Support Repository - ver. 44.0.0

I setup all as in official site for Espresso:

https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html

I try to write instrumentation test (Espresso) in package androidTest. So I create StringUtilAndroidTest in folder src/androidTest/java/com/mycompany/

My StringUtilAndroidTest code:

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

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

    @Test
    public void myTest() {
        assert(true);
    }
}

In my build.gradle:

   android.defaultconfig {
 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

my dependencies:

   testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1

But in StringUtilAndroidTest I get compile error:

@RunWith(AndroidJUnit4.class)

Cannot resolve symbol RunWith

Why?

like image 468
Alexei Avatar asked Nov 16 '25 10:11

Alexei


2 Answers

You probably have missed some dependency.

//App's dependencies, including test
compile 'com.android.support:support-annotations:22.2.0'

// Testing-only dependencies
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'junit:junit:4.12'

testCompile 'junit:junit:4.12'

Hope this will fix your code.

like image 83
Ashish M Avatar answered Nov 17 '25 23:11

Ashish M


Short answer: add this to your dependencies and you're golden.

androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'

Long answer:

In the default configuration, an Android Studio project has two different testing "variants": test & androidTest. The former uses the 'src/test/java', and the latter 'src/androidTest/java' (which is your scenario).

There's a big difference between the two: androidTest needs an emulator or a device to run, and test doesn't. This means that test is much faster to run (usually a couple seconds on the IDE), but it doesn't have access to the Android Framework (like Activities, Contexts & etc). On the other hand, androidTest takes much longer to run (not to mention the waiting time for the emulator itself), but it does have the Android framework (since it's running in one).

Since they're two separate variants, you need to declare their dependencies separately as well. testCompile and androidTestCompile each adds that dependency only to their own variant. To have JUnit on both, you have to declare to dependency on both - essentially "repeating" the line.

P.S.: Note that when you use compile, that adds it to all variants, so you don't have to repeat non-test dependencies.

like image 40
Bill Avatar answered Nov 18 '25 00:11

Bill