gradle looks like:
apply plugin: \'com.android.application\'
android {
compileSdkVersion 25
buildToolsVersion \"25.0.2\"
defaultConfig {
In my case the problem was solved by putting the below line:
@RunWith(AndroidJUnit4ClassRunner.class)
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.
Try moving your test file to the androidTest folder.
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
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
:
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.
There are two types of tests in Android:
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 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.