Error: Runtimeexception when trying to mount test case

Asked

Viewed 133 times

0

I’m trying to set up a simple test using espresso and the following errors appear:

Erro

Code:

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.core.deps.guava.annotations.VisibleForTesting;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.FrameLayout;

import com.google.gson.JsonObject;
import com.holandago.urbbox.b.LoginActivity;
import com.holandago.urbbox.b.MainActivity;
import com.holandago.urbbox.b.R;
import com.holandago.urbbox.b.model.user.User;
import com.holandago.urbbox.b.ride_details.RideDetailsFragment;
import com.holandago.urbbox.b.utils.JsonUtils;
import com.mapbox.mapboxsdk.maps.SupportMapFragment;

import org.junit.Before;
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.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
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 org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MapsActivityTest extends AppCompatActivity {

    @Rule
    public ActivityTestRule<LoginActivity> activityTestRule = new ActivityTestRule<>(LoginActivity.class);

    @Test
    public void TestEmail(){
        onView(withId(R.id.contact)).perform(click()).check(matches(withId(R.id.contact_us_container)));
    }

}

Gradle:

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

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

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


android {
    signingConfigs {
        config {
            keyAlias 'bAndroid'
            keyPassword 'abraao123'
            storePassword 'abraao123'
            storeFile file('../cer/android.jks')
        }
    }
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    lintOptions {
        // but continue the build even when errors are found:
        abortOnError false
    }
    defaultConfig {
        applicationId "com.holandago.urbbox.b"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 66
        versionName "2.9.2"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        debug {
            debuggable true
            testCoverageEnabled = true
        }
        release {
            debuggable false
            signingConfig signingConfigs.config
            ext.betaDistributionEmails = "[email protected], [email protected], [email protected], [email protected]"
            ext.betaDistributionReleaseNotes = "Release Notes for this build."
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'LICENSE.txt'
        exclude 'META-INF/maven/com.google.guava/guava/pom.properties'
        exclude 'META-INF/maven/com.google.guava/guava/pom.xml'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile fileTree(dir: 'libs', include: 'Parse-*.jar')
    compile('com.crashlytics.sdk.android:crashlytics:2.5.6@aar') {
        transitive = true;
    }
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:3.0'
    testCompile 'org.mockito:mockito-all:1.10.19'
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support:support-annotations:23.4.0'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    apt 'com.jakewharton:butterknife-compiler:8.0.1'
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.koushikdutta.ion:ion:1.3.8'
    compile 'com.google.code.gson:gson:2.4'
    compile 'com.parse.bolts:bolts-android:1.+'
    compile 'de.hdodenhof:circleimageview:2.0.0'
    compile 'com.mixpanel.android:mixpanel-android:4.6.4'
    compile 'com.android.support:recyclerview-v7:23.4.0'
    compile 'com.jakewharton:butterknife:8.0.1'
    androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile 'com.android.support.test:rules:0.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.+'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.+'
    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.+'
    compile('com.mapbox.mapboxsdk:mapbox-android-sdk:4.1.0-beta.1@aar') {
        transitive = true
    }
    compile('com.mapbox.mapboxsdk:mapbox-android-services:1.1.0@aar') {
        transitive = true
    }
}

Anyone who knows or has resolved please help me. Thank you very much!

testEmail error:

java.lang.Runtimeexception: Can’t create Handler Inside thread that has not called Looper.prepare()

  • Hello, Henry, hello. Please place the error message as text in the body of the question, to facilitate future searches and allow reading by screen reader users.

  • There are several, so the image. But I will put one.

  • Put at least the one in the image.

1 answer

2


Note this case below:

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.typeText;
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;

// Testes para MapsActivity
public class MapsActivityInstrumentationTest {

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

     // Verifica se no EditText tem o texto "Hello"
    @Test
    public void validaEditText() {
        onView(withId(R.id.etInput)).perform(typeText("Hello")).check(matches(withText("Hello")));
    }
}

build.Gradle

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

For more details, take a read in this article.

You can check out a list of Espresso methods on google documentation.

inserir a descrição da imagem aqui

  • There is this "startservice" in activityTestRule .. I can’t find it after the point and putting it, it is in "red".

  • It worked out here. Thank you very much!

  • @Henriquemendes if you need, give a shout! Abs

  • @Henriquemendes just wanted to let you know that what you’re doing is a business differential. Be a good developer and an excellent Ester, it’s worth it.

  • Thank you. Trying to start with TDD. To start still. Thank you again and can keep an eye that will come more questions hah

Browser other questions tagged

You are not signed in. Login or sign up in order to post.