Android. #RunWith(AndroidJUnit4.class) - cannot resolve in package "androidTest" - android

Android Studio 2.2.3
Install Android Support Repository - ver. 44.0.0
I setup all as in official site for Espresso:
https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html
I try to write instrumentation test (Espresso) in package androidTest. So I create StringUtilAndroidTest in folder src/androidTest/java/com/mycompany/
My StringUtilAndroidTest code:
#RunWith(AndroidJUnit4.class)
#LargeTest
public class StringUtilAndroidTest {
#Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);
#Test
public void myTest() {
assert(true);
}
}
In my build.gradle:
android.defaultconfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
my dependencies:
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1
But in StringUtilAndroidTest I get compile error:
#RunWith(AndroidJUnit4.class)
Cannot resolve symbol RunWith
Why?

Short answer: add this to your dependencies and you're golden.
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
Long answer:
In the default configuration, an Android Studio project has two different testing "variants": test & androidTest. The former uses the 'src/test/java', and the latter 'src/androidTest/java' (which is your scenario).
There's a big difference between the two: androidTest needs an emulator or a device to run, and test doesn't. This means that test is much faster to run (usually a couple seconds on the IDE), but it doesn't have access to the Android Framework (like Activities, Contexts & etc). On the other hand, androidTest takes much longer to run (not to mention the waiting time for the emulator itself), but it does have the Android framework (since it's running in one).
Since they're two separate variants, you need to declare their dependencies separately as well. testCompile and androidTestCompile each adds that dependency only to their own variant. To have JUnit on both, you have to declare to dependency on both - essentially "repeating" the line.
P.S.: Note that when you use compile, that adds it to all variants, so you don't have to repeat non-test dependencies.

You probably have missed some dependency.
//App's dependencies, including test
compile 'com.android.support:support-annotations:22.2.0'
// Testing-only dependencies
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
Hope this will fix your code.

Related

Android : What is the correct gradle configuration to use Mockito to support espresso and unit test both?

I am running couple of tests using Espresso and some tests are plain junit, both tests require mockito.
Wondering how to include mockito correctly to support both , if I use one of below configuration other wouldn't work.
1) testCompile "org.mockito:mockito-core:2.+"
2) androidTestCompile "org.mockito:mockito-core:2.+
I currently configure and run them in separate build types, because of the dexmaker conflict. Not sure whether it is the intended/correct way or there is a better way of doing it.
So assuming you have already a "debug" build type, define another, i.e. "espresso". Then the build gradle would look like:
android {
...
testBuildType 'espresso'
}
...
dependencies {
...
def mockito = "org.mockito:mockito-core:2.+"
espressoCompile(mockito) {
exclude module: 'hamcrest-core'
}
espressoCompile("com.crittercism.dexmaker:dexmaker-mockito:$dexMakerVersion") {
exclude module: 'hamcrest-core'
}
espressoCompile "com.crittercism.dexmaker:dexmaker-dx:$dexMakerVersion"
androidTestCompile "com.android.support.test.espresso:espresso-intents:$espressoVersion"
androidTestCompile "com.android.support.test.espresso:espresso-contrib:$espressoVersion"
androidTestCompile mockito
testCompile mockito
}
When you want to run the tests, you need to call the targeted gradle scripts, i.e. 'testDebug' or 'installEspressoAndroidTest'.
Or within Android Studio, you need to run the espresso tests after switching to "espresso" build variant.
Hope it helps or give you some idea about it.

Unable to resolve Android JUnit test runner

I am trying to add an instrumentation test to our project, but it seems that Gradle doesn't properly add the Android JUnit Test Runner to the project's classpath. The test depenencies of my gradle build file looks like this:
testCompile 'junit:junit:4.12'
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
We are using the latest version of the support library (24.0.0), but the current version of the test runner (JUnit runner) and Espresso use version 23.1.0. To resolve the version conflict, I force the runner (and Espresso) to use the newer version (I understand the implications, but we can't use the older version):
androidTestCompile 'com.android.support:support-v4:24.0.0'
androidTestCompile 'com.android.support:appcompat-v7:24.0.0'
androidTestCompile 'com.android.support:support-v13:24.0.0'
androidTestCompile 'com.android.support:recyclerview-v7:24.0.0'
androidTestCompile 'com.android.support:design:24.0.0'
androidTestCompile 'com.android.support:support-annotations:24.0.0'
and:
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:24.0.0'
}
}
However, for some reason, Gradle doesn't add the runner package (under android.support.test). So
import android.support.test.runner.AndroidJUnit4;
throws an error: cannot resolve symbol 'runner'. Have cleared Android Studio's cache, restarted the IDE, cleared Gradle's cache (both project and global), all with no luck. Does anyone know what's going on?
try adding:
androidTestCompile 'com.android.support.test:rules:0.5'
This is my old question but thought it might help to explain the issue and the solution. The issue was with the name of the debug build variant: if you name your debug build variant anything but debug, make sure to notify Android's Gradle plugin by adding testBuildType "yourDebugBuildVariantName" to your build.gradle script (your app module's build.gradle not the project global) in the android{} section, or rename your debug build variant to just debug. If you have multiple debug build variants, you need to specify one of them on which you'd like to run your tests, like: testBuildType armDebug:
apply plugin: 'com.android.application'
...
android {
testBuildType "myDebug" <-
compileOptions { ... }
sourceSets { ... }
signingConfigs { ... }
}
Even with this explicit config, Gradle occasionally seems to have issues with running instrumentation tests. The best way to workaround this is to rename your debug build variant (the one you run your tests on) to debug if this is a feasible option for you.

android studio: how to change from junit 3 to junit 4

My android studio is running JUnit 3. How do I change it to JUnit 4?
I believe my gradle file is correct.
android {
…
defaultConfig {
…
testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
}
…
}
dependencies {
// Required -- JUnit 4 framework
testCompile 'junit:junit:4.12'
// Optional -- Mockito framework
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.android.support:support-annotations:23.2.0'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
// Optional -- Hamcrest library
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with Espresso
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
// Optional -- UI testing with UI Automator
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}
I am using the Android Studio 2.1.1
update
Actually it turns out I am using JUnit 4 but for whatever reason I have to prefix my test cases with test as in testSomeMethodForX as opposed to simply someMethodForX. I thought in JUnit 4, this prefixing was no longer necessary?
If your classes still extend TestCase (or another class that itself extends TestCase) then JUnit 4 runs them a JUnit 3 tests and therefore you have to prefix them with test. Don't extend TestCase anymore and they are run if the #Test annotation is present.

Differences between testCompile and androidTestCompile for junit imports

What is the difference between these two import statements? (in build.gradle):
testCompile 'junit:junit:4.12'
androidTestCompile 'junit:junit:4.12'
There's a great answer here explaining the difference:
Simply testCompile is the configuration for unit tests (located in src/test) and androidTestCompile is used for the test API (located in src/androidTest).
...
The main distinction between the two is the test sourceset runs in a regular Java JVM, whereas the androidTest sourceset tests run on an Android device (or an emulator).

In Android, why do I need to use androidTestCompile for the "test" sourceSet?

So I have a library module that wraps an API, and I want to write some tests. I want to import things like JUnit and MockWebServer, but only for the test sourceSet, and not for the androidTest one, as I want to use the former because the latter would cause the tests to be run in an android device or AVD, which I do not want. Therefore, I have this in my gradle file:
sourceSets {
main {
test {
setRoot('src/test')
}
}
}
...
dependencies {
...
testCompile 'junit:junit:4.12'
testCompile 'com.squareup.okhttp:mockwebserver:2.4.0'
}
However, this will not work, and instead I will have to import the dependencies as androidTestCompile ones. Why is this?
You should have a structure like this:
root
module
src
main
test
And all you need in your build.gradle is (without setting source sets):
dependencies {
// Unit testing dependencies.
testCompile 'junit:junit:4.12'
testCompile 'com.squareup.okhttp:mockwebserver:2.4.0'
}
You can check in the official repo from Google:
A collection of samples demonstrating different frameworks
collection of Google's Android testing tools
If you would like to use unit test and Instrumentation tests you should have:
root
module
src
main
test
androidTest
In this case your build.gradle should be:
dependencies {
// Dependencies for local unit tests
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-all:1.10.19'
testCompile 'org.hamcrest:hamcrest-all:1.3'
// Android Testing Support Library's runner and rules
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
}

Categories

Resources