Android Espresso : cannot resolve symbol AndroidJUnit4.class

redrom picture redrom · Aug 24, 2015 · Viewed 25.6k times · Source

I'm trying to create Espresso UI test inside the new Android project but I faced with the following problem.

If I tried to create a empty test class:

import android.content.Intent;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;


@RunWith(AndroidJUnit4.class)
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {

}

I always get this error message:

cannot resolve symbol AndroidJUnit4.class

And almost all imported libraries are marked as unused.

build.gradle file is containing the following:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "com.some.thing.xxx"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
    packagingOptions {
        exclude 'LICENSE.txt'
    }
}

repositories {
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    maven { url "https://jitpack.io" }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.google.android.gms:play-services:7.8.0'
    compile 'com.mcxiaoke.volley:library:1.0.18'
    compile 'com.orhanobut:logger:1.11'
    // App dependencies
    compile 'com.android.support:support-annotations:23.0.0'
    // TESTING DEPENDENCIES
    androidTestCompile 'com.android.support.test:runner:0.3'
    // Set this dependency to use JUnit 4 rules
    androidTestCompile 'com.android.support.test:rules:0.3'
    // Set this dependency to build and run Espresso tests
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
    // add this for intent mocking support
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'
    // add this for webview testing support
    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2'
    // Set this dependency to build and run UI Automator tests
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
}

If I put these setting on my other test project it works, so I don't know what can be wrong?

I've followed this tutorial:"

http://www.vogella.com/tutorials/AndroidTestingEspresso/article.html

And I've tried to resolve it by following SO question: Cannot resolve symbol 'AndroidJUnit4'

But without the luck.

Many thanks for any advice.

Answer

passerby picture passerby · Sep 7, 2015

I've tried the same tutorial from vogella too and ran into many issues. One of the first issues I ran into was a dependency clash between the annotation versions of v23 libs and the Espresso libs.

Then I found another recently updated tutorial from Roger Hu "UI Testting with Espresso". I noticed a remark that Espresso is not supporting Marshmallow yet.

The dependencies were added as follows:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
    // Necessary if your app targets Marshmallow (since Espresso
    // hasn't moved to Marshmallow yet)
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test:runner:0.3') {
    // Necessary if your app targets Marshmallow (since the test runner
    // hasn't moved to Marshmallow yet)
    exclude group: 'com.android.support', module: 'support-annotations'
}

This solved my dependency conflict and I didn't see any of the rest of the issues occurring.