Skip to content Skip to sidebar Skip to footer

Can't Select Test Class In Edit Configuration

I have created an Espresso/UIAutomator unit test. However, when I try to run it, Android Studio won't recognize it. The button to select TestLogin.java is greyed out. I'm using An

Solution 1:

Add one more package with name test like that: com.greenrobot.yesorno.test. Then put TestLogin test inside it. This should solve your problem.

ALSO: ActivityInstrumentationTestCase2 is deprecated and can't be used together with ActivityTestRule. Leave only ActivityTestRule and remove extends part and constructor. I'd also suggest to add setUp() method with @Before annotation and instantiate mDevice there, so it will be available for each test you'll have in test class.

UPDATE: also remove this, sync and clean:

 androidTest {
     java.srcDirs = ['androidTest/java']
 }

Solution 2:

I hired a developer on Upwork to look into this for me. The main problem was the my androidTest src directive was missing a 'src'

    androidTest {
        java.srcDirs = ['src/androidTest/java']
    }

My build.gradle:

    buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'




android {

    aaptOptions.setProperty("cruncherEnabled", false)
    sourceSets {

        androidTest {
            java.srcDirs = ['src/androidTest/java']
        }
    }
    lintOptions {
        // set to true to turn off analysis progress reporting by lint
        quiet true// if true, stop the gradle build if errors are found
        abortOnError false// if true, only report errors
        ignoreWarnings true
    }
    productFlavors {
        // The actual application flavor
        production {
            minSdkVersion 15
        }
        // Test application flavor for uiautomatior tests
        myTest {
            minSdkVersion 18
        }
    }
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    defaultConfig {
        applicationId "com.greenrobot.yesorno"
        minSdkVersion 15
        targetSdkVersion 22
        multiDexEnabled = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/maven/com.google.guava/guava/pom.properties'
        exclude 'META-INF/maven/com.google.guava/guava/pom.xml'
    }
}

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
}

repositories {
    maven { url "http://jzaccone.github.io/SlidingMenu-aar" }

}
repositories {
    maven { url "http://dl.bintray.com/populov/maven" }
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}

defautobahn_version='0.5.2-SNAPSHOT'

dependencies {
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.robbypond:mopub-android-sdk:3.9.0'
    compile 'com.facebook.android:facebook-android-sdk:4.7.0'
    compile 'com.jeremyfeinstein.slidingmenu:library:1.3@aar'
    compile 'com.android.support:support-v4:23.1.1'
    compile files('libs/FlurryAgent.jar')//compile files('libs/autobahn-android-0.5.2-SNAPSHOT.jar')
    compile 'de.tavendo:autobahn-android:' + autobahn_version
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile project(':viewpagerindicator')
    compile files('libs/gcm.jar')
    compile 'com.jakewharton.timber:timber:3.1.0'
    compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
        transitive = true;
    }
    compile 'com.squareup.okhttp3:okhttp:3.0.1'
    compile 'com.android.support:support-annotations:23.1.1'
    compile 'com.google.guava:guava:18.0'// Testing-only dependencies
    androidTestCompile 'com.android.support:support-annotations:23.1.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0'
    androidTestCompile 'junit:junit:4.12'
}

My TestLogin.java

package com.greenrobot.yesorno.test;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.Until;
import android.test.suitebuilder.annotation.LargeTest;

import com.greenrobot.yesorno.Home;

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

importstatic android.support.test.espresso.Espresso.onView;
importstatic android.support.test.espresso.assertion.ViewAssertions.matches;
importstatic android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
importstatic android.support.test.espresso.matcher.ViewMatchers.withId;

import com.greenrobot.yesorno.R;


/**
 * Created by andytriboletti on 1/15/16.
 */@RunWith(AndroidJUnit4.class)@LargeTestpublicclassTestLogin  {

    private UiDevice mDevice;

    privatestaticfinalStringPACKAGE_NAME="com.greenrobot.yesorno";
    privatestaticfinalintLAUNCH_TIMEOUT=5000;

    @Rulepublic ActivityTestRule<Home> mActivityRule = newActivityTestRule(Home.class);

    publicTestLogin() {
        super();
    }

    @BeforepublicvoidinitTest() {
        // Initialize UiDevice instance
    
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        // Start from the home screen
        mDevice.pressHome();

        // Wait for launcherStringlauncherPackage= mDevice.getCurrentPackageName();
        mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);

        // Launch the appContextcontext= InstrumentationRegistry.getContext();
        Intentintent= context.getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME);

        // Clear out any previous instances
    
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);

        // Wait for the app to appear
    
        mDevice.wait(Until.hasObject(By.pkg(PACKAGE_NAME).depth(0)), LAUNCH_TIMEOUT);
    }

    @TestpublicvoidtestLogin() {
        onView(withId(R.id.welcome)).check(matches(isDisplayed()));

//        onView(withText("Hello world!")).check(matches(isDisplayed()));//onView(withId(R.id.changeTextBt)).perform(click());

    }
}

I spent 10 minutes trying to get TestLogin to format as code and it wouldn't work so I created a pastebin. http://pastebin.com/9W3pT4rH Stackoverflow, you should allow me to select files, and then display them as code.

Post a Comment for "Can't Select Test Class In Edit Configuration"