سؤال

I'm running Android Studio 0.5.0 with Gradle 1.11. I'm trying to install Espresso library from com.jakewharton.espresso:espresso:1.1-r2. For some reason AS couldn't recognize Espresso classes after project synchronization. So every time I try to import import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView; inside androidTest folder files it marks it as invalid.

Here's my build.gradle:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.2'

    defaultConfig {
        minSdkVersion 14

        targetSdkVersion 19
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.squareup.dagger:dagger-compiler:1.2.1'
    compile 'com.squareup.dagger:dagger:1.2.1'

    androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r2') {
        exclude group: 'com.squareup.dagger'
    }
}

External libraries:

external libraries

هل كانت مفيدة؟

المحلول

So this is basically a bug with Android Studio (i'm guessing).

Reference:

  1. Issue raised in adt-dev groups
  2. Actual bug - #66841

Workaround (until the bug is fixed):

Add a duplicate provided dependency in your gradle file like so:

dependencies {
    // ...

    provided 'com.jakewharton.espresso:espresso:1.1-r2'
    androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r2') {
        exclude group: 'com.squareup.dagger'
    }
}

نصائح أخرى

This issue was driving me crazy. And seems like it's a known bug in Android Studio. In my case it resolved after I've changed Build Type from Release to Debug for the parent app. Hope this may be helpful for someone

Espresso 2.0

Recently Espresso 2.0 was released making it now part of the Android Support Library. This was announced on the android dev blog.

Setup Guide

With that they also linked an updated setup guide. There you can find instructions to configure from scratch or to update your existing espresso config for 2.0.

Other Tips

Changes are the above 2 links contain all information you need. If not I have listed some common mistakes below

Upgrade Android Studio to 1.0.*

Start by upgrading your android Studio build. You should be able to get at least 1.0 from the stable builds channels (=default). So just use the menu option Android Studio > Check for updates... .

To get the latest from the latest you can also go into Preferences, search for updates and change the channel to canary channel.

Update Android Support Library to v 11+

Espresso was included in the Support library from version 11 so you have to get at least that version. Check for updates using the Android SDK manager. The Support Library is within the Extras tree at the bottom.

New dependencies and namespace

If upgading from an older espresso release you'll have to update the dependencies and the namespace. For new projects just add these to the dependencies in your build.gradle file.

dependencies {
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}

And since the namespace changed you'll have to update all imports:

android.support.test.espresso

Note that it's easier to use static imports. Some commonly used imports as an example:

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;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;

For asserts use hamcrest, again some examples:

import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;

Instrumentation runner

The test runner needs to be configured in both your build.gradle file within defaultConfig and the run configuration used to launch your tests from Android Studio.

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

And in your run configuration use this as instrumentation runner (full class name only):

android.support.test.runner.AndroidJUnitRunner

Example test case

And an example test case to finish with. Note that MainActivity is your actvitiy you want to test. The tests themselves are public methods that start with test, like testListGoesOverTheFold in the below example.

@LargeTest
public class HelloWorldEspressoTest extends ActivityInstrumentationTestCase2<MainActivity> {

    public HelloWorldEspressoTest() {
            super(MainActivity.class);
        }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        getActivity();
    }

    public void testListGoesOverTheFold() {
        onView(withText("Hello world")).check(isDisplayed());
    }
}

For more information on writing tests visit the espresso start guide.

Six months later this is still an issue and the bug referenced by the original responder has been reopened: https://code.google.com/p/android/issues/detail?id=66841 and given higher priority. I have not yet been able to get Android Studio to recognize Espresso classes and using the "provided" scope for dependencies didn't work to fix the issue for me. (Using AS 0.8.6 and Gradle 0.12.2)

according to http://tools.android.com/tech-docs/new-build-system/user-guide only one build type is tested, by default it is debug Build Type.

So check that you are using debug build variant and rebuild the application. On other build types all your androidTest dependencies will not be visible.

If you need to test your current build type you can do something like this:

android { ... testBuildType "staging" }

Android Studio 1.5.1 didn't realize onView() or onData() methods in my case. I just did a static import of the Espresso class and all it's methods.

I added the below line and everything worked perfectly.

import static android.support.test.espresso.Espresso.*;

You're not specific about what source file you're seeing the error in, but based on my testing, I think you're trying to access the Espresso classes from one of your main application classes (inside src/main/java/). If so, that won't work, because you've included Espresso via an androidTestCompile dependency include, which makes it accessible only to your test classes, which must be under src/androidTest/java.

I've tried every solution what guys guessed above and still got the class not found error.

I figured out my solution, it saved my day. So if you open the project tab on left to your project folders in Studio and check the build variants you can see that, your project is set to unit test. You have to re-set it to Android Instrumentation Tests and make sure your test.java file is under **src\androidTest\java**

try these dependencies:

implementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

I was having this issue and I went through and added every espresso library I could add to my gradle file. Wait for the yellow lightbulb to come on and then click add library and search for the espresso libraries you don't have. I don't know if this will work for everyone but it worked for me.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top