Differences between testCompile and androidTestCompile for junit imports - android

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).

Related

Mockito+PowerMock gradle configuration

I need to use in my android instrumented tests mockito and powermock.
The main problem is that both of them have some problems with configuring it in gradle because of conflicts and other stuff.
Maybe somebody who has working configuration of .gradle file for mockito+powermock in android instrumented tests could share it?
This is my gradle configuration to use mockito and powerMock:
dependencies {
...
/**Power mock**/
testCompile "org.powermock:powermock-core:1.7.3"
testCompile "org.powermock:powermock-module-junit4:1.7.3"
testCompile "org.powermock:powermock-api-mockito2:1.7.3"
/**End of power mock **/
}
NOTE: I had to remove the mockito dependency in order to make it works:
//Remove this line
testImplementation "org.mockito:mockito-core:2.13.0"
Here is the configuration I am using and it's working perfectly fine.
after 1.7.0 powermock-api-mockito change to powermock-api-mockito2
testImplementation 'org.mockito:mockito-all:1.10.19'
testImplementation "org.powermock:powermock-module-junit4:2.0.7"
testImplementation "org.powermock:powermock-module-junit4-rule:2.0.7"
testImplementation "org.powermock:powermock-api-mockito2:2.0.7"
testImplementation "org.powermock:powermock-classloading-xstream:1.6.6"

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

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.

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.

Android Espresso testing 'Cannot resolve symbol 'InstrumentationRegistry''

I'm trying to import
import android.support.test.InstrumentationRegistry;
my build.gradle file
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support.test:rules:0.2'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
in default config:
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Is there a library I'm missing here? I'm trying to import InstrumentationRegistry but it doesn't recognise it!
Check what kind of test do you use.
InstrumentationRegistry used for Instrumented tests
that use emulator or device and they are placed in src/androidTest and use config androidTestCompile.
If you use Local unit tests for JVM from folder src/test you should use
config testCompile
testImplementation 'com.android.support.test:runner:1.0.2'
After that you can import InstrumentationRegistry, but you will get other errors at run-time.
try
compile 'com.android.support.test:runner:0.2'
instead of
testCompile 'com.android.support.test:runner:0.2'
it seems, com.android.support.test had been recently excluded from some other package (no clue which one), which also resulted in android.support.test.InstrumentationRegistryto be unknown; not excluding it from com.android.support.test:runner fixed the issue for me.
androidTestImplementation ("com.android.support.test:runner:1.0.2") {
// exclude group: "com.android.support.test"
exclude group: "com.android.support"
}
basically, androidTestImplementation needs to contain com.android.support.test once.
Another alternative - you can also add androidx.test:monitor dependency where the class InstrumentedRegistry is.
androidTestImplementation 'androidx.test:monitor:x.y.z'

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