java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass)

no instrumentation registered! must run under a registering instrumentation.
android mock context
unit testing android studio
applicationprovider getapplicationcontext
junit android tutorial

gradle looks like:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "com.google.developer.taskmaker"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

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

configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-annotations:25.2.0'
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support:recyclerview-v7:25.2.0'
    compile 'com.android.support:design:25.2.0'
    compile 'com.android.support:preference-v7:25.2.0'
    debugCompile 'im.dino:dbinspector:3.4.1@aar'
    // Android JUnit Runner
    compile 'com.google.android.gms:play-services-appindexing:8.4.0'
    // Android runner and rules support

    // add this for intent mocking support
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'

    // add this for webview testing support
    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2'
    compile 'com.android.support.test:runner:0.5'
    compile 'com.android.support.test:rules:0.5'
    compile 'com.android.support.test.espresso:espresso-core:2.2.2'
}

Unit test case looks like

@RunWith(AndroidJUnit4.class)
public class TestClass {

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

    @Test
    public void buttonClick(){
        onView(withId(R.id.fab)).perform(click()).check(matches(isDisplayed()));
    }

}

Error message looks like:

java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass)

    at org.junit.runners.model.InitializationError.<init>(InitializationError.java:38)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:111)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
    at com.intellij.junit4.JUnit46ClassesRequestBuilder.collectWrappedRunners(JUnit46ClassesRequestBuilder.java:90)
    at com.intellij.junit4.JUnit46ClassesRequestBuilder.getClassesRequest(JUnit46ClassesRequestBuilder.java:51)
    at com.intellij.junit4.JUnit4TestRunnerUtil.buildRequest(JUnit4TestRunnerUtil.java:91)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:95)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


Process finished with exit code -1

I have already check other answers but not getting solution.Please tell where thing goes wrong. When running this code I am getting error

Cause 1: placing tests in the wrong folder

This error can be replicated by making a new Application with the default template in Android Studio and copying the auto-generated ExampleInstrumentedTest from the androidTest folder to the test folder:

Here's what the error looks like:

Note that in order to replicate the problem you would also have to incorrectly add dependencies to the module-level build.gradle:

Cause 2: wrong test configuration

Local unit tests and instrumented tests have different Run configurations. If you click Edit Configurations like below:

You should see your instrumented tests under Android Instrumented Tests and your local unit tests under the Android JUnit like in the illustrations below:

For Android Instrumented Tests the config should look something like below:

For Android JUnit:

When you run a test, Android Studio will create a run config for you. If the run config is in the wrong category, check you have your test and androidTest folders correct and then you can delete the run config from the Edit configurations and run the test again. If you have set up correctly, Android Studio will use the correct type of run configuration this time.

Explanation

There are two types of tests in Android:

  1. Instrumented tests
  2. Local unit tests
Instrumented tests

Instrumented tests are tests that are designed to run on a handset or emulator. These are tests where you need access to a fully-functional part of the Android library (like a real Context for example). These need to go in the androidTest folder and dependencies for these tests (e.g., Espresso, com.android.support.test.rules:0.5) will be prefixed with androidTestCompile in your build.gradle.

Here is an example instrumented test:

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

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

import static org.junit.Assert.assertEquals;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.gyaltsab.myapplication", appContext.getPackageName());
    }
}
Local unit tests

Local unit tests are tests that you can run in your IDE. They normally don't depend on any part of the Android library not available on a standard JVM (e.g., they won't depend on Context). The dependencies for these go in the testCompile part of your build.gradle.

Here is an example unit test:

import org.junit.Test;

import static org.junit.Assert.*;

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
public class ExampleUnitTest {
    @Test
    public void addition_isCorrect() throws Exception {
        assertEquals(4, 2 + 2);
    }
}

Note that local unit tests do not need the @RunWith(AndroidJUnit4.class) annotation in the class declaration. Please see the official docs for a more complete explanation.

Custom runner class AndroidJUnit4 should have a public , java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass) Hi, I Have created the exact same test class as "CalculatorTest.java".. But when i am trying to run from Android studio, i am facing the following exception.. java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass)

I had the same problem. I'm working in Android studio 2.3.1. I checked my run configurations. Under Run->Edit Configurations and discovered the test I was trying to run as an instrumented test was under the Android JUnit tests category, even though I had it in the androidTest directory in my project. I added an Android Instrumented Test (hit the plus button in the corner) and set it to point to the test I was trying to run. That fixed it for me.

AndroidJUnitRunner, I have an Android project that I am trying to run Instrumentation tests for using AndroidStudio, but when I try to run the whole class file for the tests, I get the exceptions as stated below : java.lang. Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4 (​Class testClass)  java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass) at org.junit.runners.model.InitializationError.&lt;init&

Try moving your test file to the androidTest folder.

Type Android Junit4 not present exception, java.lang.ExceptionCustom runner class SWTBotJUnit4Suite should have a public constructor with signature SWTBotJUnit4Suite(Class testClass) Custom runner class AndroidJUnit4 should have a public constructor with signature  java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass) if you have any idea what i do wrong please tell me, because i have the same behavior with my own project

In my case the problem was solved by putting the below line:

@RunWith(AndroidJUnit4ClassRunner.class)

java.lang.Exception: Custom runner class SWTBotJUnit4Suite , Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass) and for sure it is a built-in class  [ Natty] android java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass) By: Mohamed harmoush 2.0; [ Natty ] ios UICollectionView Header and Footer very strange behavior By: narbodon 3.0 ;

solution:- you have to change the test folder from test to androidTest and edit the test configuration.(or move the test file from test to androidTest edit the test configuration)

I found the solution in this video: https://youtu.be/TGU0B4qRlHY?t=9m36s

and here:- https://stackoverflow.com/a/30172064/5723125

User David Rawson, java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass) · stackoverflow.com. When I run:./ gradlew clean testDebug --tests = '*.SimpleTest' I get this exception: java. lang. Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4 (Class testClass) Why does it think 'AndroidJUnit4' is a custom runner class, when it's a final class?

Custom JUnit4 Test Runners, At runtime, JUnit will pass the target test class to this constructor. RunNotifier is used for firing events that have information about the test progress. Java.lang.Exception: La clase de corredor personalizada AndroidJUnit4 debe tener un constructor público con la firma AndroidJUnit4 (clase testClass)

java.lang.Exception : 사용자 정의 러너 클래스 AndroidJUnit4에는 , Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass) at org.junit.runners.model. Причина 1: размещение тестов в неправильной папке. Эту ошибку можно реплицировать, создав новое приложение с шаблоном по умолчанию в Android Studio и скопировав автоматически созданный ExampleInstrumentedTest из папки androidTest в test папку:

Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass) at org.junit.runners.model. java. lang. Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4 (Class testClass)

Comments
  • Can you show the imports for your "unit test case"?
  • Also, those testing support dependencies should be in androidTestCompile, not compile. What happens if you change them to androidTestCompile?
  • After double-checking both of your causes, I am still getting this error...
  • Perfect answer! Thanks!