Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Espresso: Test filtering is not supported for given version of JUnit. Please upgrade JUnit version to at least 4.6

I' m really afraid to post this, but I haven't found anything related to it on google so I guess it's a noob question.

I want to use Espresso to test my android apps.

I' ve tried to download the sample project, create a simple project and implement it as described in the android developer site, but I can't get it up and running.

  • I have build tools 22.0.1
  • Support library 22

After manually adding the static imports, I' ve resolved all the compilation problem with my example test. But when I run it, I get this:

:app:cleanTest UP-TO-DATE
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72200Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42200Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJava UP-TO-DATE
:app:preCompileDebugUnitTestJava
:app:compileDebugUnitTestJava UP-TO-DATE
:app:compileDebugUnitTestSources UP-TO-DATE
:app:mockableAndroidJar UP-TO-DATE
:app:assembleDebugUnitTest UP-TO-DATE
:app:testDebug
:app:testDebug FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:testDebug'.
> Test filtering is not supported for given version of JUnit. Please upgrade JUnit version to at least 4.6.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 32.847 secs
Test filtering is not supported for given version of JUnit. Please upgrade JUnit version to at least 4.6.

This is my test class:

package com.example.federicoponzi.testingexample;

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;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import android.app.Activity;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
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 android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;

import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.matcher.ViewMatchers;


/**
 * Created by FedericoPonzi on 10/04/2015.
 */

import org.junit.Test;

@RunWith(AndroidJUnit4.class) //copied from a google example
@LargeTest
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
    public MainActivityTest(){
        super(MainActivity.class);
    }
    Activity mActivity;
    @Override
    protected void setUp() throws Exception {
        super.setUp();

        mActivity = getActivity();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());

    }
    private final String STRING_TO_TYPE = "Hello, Testing";
    @Test
    public void testChangeText_sameActivity()
    {
        onView(withId(R.id.edittextview)).perform(typeText(STRING_TO_TYPE), closeSoftKeyboard());
        onView(withId(R.id.edittextview)).check(matches(withText(STRING_TO_TYPE)));

    }

}

Build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.federicoponzi.testingexample"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.0.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}

edit: New error after gradle update:

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:testDebug'.
> No tests found for given includes: [com.example.federicoponzi.testingexample.MainActivityTest]
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 7.246 secs
No tests found for given includes: [com.example.federicoponzi.testingexample.MainActivityTest]
like image 688
Federico Ponzi Avatar asked Dec 11 '22 00:12

Federico Ponzi


1 Answers

Important to know:

androidTestCompile - androidTest folder - ui testing

testCompile - test folder - unit testing

For Unit Test:

Add:

testCompile 'junit:junit:4.12'

To:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.0.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    testCompile 'junit:junit:4.12' // <-- added
}

For your Espresso Test:

Remove Junit and Annotations.

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
    public MainActivityTest(){
        super(MainActivity.class);
    }
    Activity mActivity;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        mActivity = getActivity();
        // injectInstrumentation(InstrumentationRegistry.getInstrumentation()); // not sure what this is 

    }
    private final String STRING_TO_TYPE = "Hello, Testing";

    public void testChangeText_sameActivity()
    {
        onView(withId(R.id.edittextview)).perform(typeText(STRING_TO_TYPE), closeSoftKeyboard());
        onView(withId(R.id.edittextview)).check(matches(withText(STRING_TO_TYPE)));
    }
}

Examples:

https://github.com/googlesamples/android-testing/blob/master/espresso/BasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample/ChangeTextBehaviorTest.java https://github.com/jaredsburrows/AndroidGradleTemplate

Official Documentation: https://developer.android.com/tools/testing-support-library/index.html and https://code.google.com/p/android-test-kit/wiki/AndroidJUnitRunnerUserGuide

like image 154
Jared Burrows Avatar answered Apr 30 '23 15:04

Jared Burrows