4

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?

2
  • Apart from adding the androidTestCompile lines to your build.gradle, did you also remember to import the class on top of the Java file? i.e. import org.junit.runner.RunWith; Commented Feb 22, 2017 at 20:56
  • import org.junit.runner.RunWith; - cannot resolve symbol 'RunWith' Commented Feb 23, 2017 at 8:08

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have just tested that. and they did work in my case.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.