I want to setup my project for unit testing.
I tried to follow the instructions on Android's page:
// Unit testing dependencies
testCompile 'junit:junit:4.12'
// Set this dependency if you want to use Mockito
testCompile 'org.mockito:mockito-core:1.10.19'
// Set this dependency if you want to use Hamcrest matching
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
However, when doing that and creating a test, I get:
"Cannot resolve symbol 'junit'"
"Cannot resolve symbol 'mockito'"
In Vogel's tutorial, a lot more dependencies are required, and I want the bare minimum.
Also, using Vogel's tutorial, I get:
Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (22.2.1) and test app (22.2.0) differ.
So my question is: How can I get the dependencies from Android's page to work?
The support-annotations issue is a known one. You can find the info in their issue tracker. To workaround it, in the main (app, not module) build.gradle file, section allprojects add
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:22.2.0'
}
(see answer #11 in the same link).
About the extra dependencies, you are going to need dexmaker and dexmaker-mockito for using mockito for your tests in devices/emulator, as they run on a Dalvik VM that expects .dex files, while mockito generates .class files. Unit testing as in the newest unit testing added to Android Studio, runs in your local JVM so it should probably run without dexmaker, but I cannot confirm this as of yet.
Related
gradle sync keeps failing with my android studio. need help
https://github.com/linkedin/dexmaker This is the open source i am trying to use called dexmaker.
I tried to download by using
androidTestCompile 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.0'
but it gets errors like this
Failed to resolve: com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.1
I finally tried
androidTestCompile 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.19.1'
this works but in my java code
The problem is even though i succeeded on syncing gradle, i still can't use the opensource.
DexMaker dexMaker = new DexMaker();
DexMaker gets red lines and if i click it it says
cannot resolve symbol 'DexMaker'
what's the problem?
I'm not sure, but as I understand your situation:
You add dependency using androidTestCompile directive. That means that this package will become available only inside android test classes. For more understanding each dependency could be added in three ways: compile, testCompile and androidTestCompile.
compile : Dependency is available everywhere in your application code.
testCompile : Dependency available only in regular tests.
androidTestCompile : Dependency available only in android tests.
So if you want that dependency to be available in your application - replace androidTestCompile with compile. But I not sure, that you SHOULD do that, cause that library is for tests.
P.S. Using compile directives is deprecated and you should use implementation, testImplementation and androidTestImplementation.
I feel dumb for even having to ask this, but I can't find an answer anywhere. I'm trying to write simple unit tests that test static methods from my Android app and I've already added
// Required -- JUnit 4 framework
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
To my app build.gradle file, but org.junit.test annotations or anything related to Junit4 will NOT resolve. There is no "Unit Test" build variant either.
Can anybody assist me? Thanks.
looks like you have not download dependencies:
try run "gradle build"
or sync gradle project if your IDE has this option
I've been stuck on this problem all day, I'm hoping someone can help me get my program running.
I am building an Annotation Processor for Android, it currently has two modules: "mymodule" and "mymodule-compiler"
mymodule-compiler depends on mymodule:
dependencies {
compile project(':mymodule')
...
}
mymodule uses the netflix "provided" plugin for Android:
apply plugin: 'nebula.provided-base'
dependencies {
provided 'com.google.android:android:4.1.1.4'
}
This actually works perfectly, however when I run my compiler unit test suite, a lot of tests fail because the Android dependency specified is v4.1.1.4 and I actually need v6.0. v6.0 is not available on Maven (and looks like it never will be because Google haven't updated it since 2012).
I found out that the Android SDK provides a "Unit Test" JAR version of Android for each release, which is the entire SDK "stubbed out". I assume the versions on Maven are stubbed out too(?). I can easily grab that JAR and put it in my project under libs/.
Now when I update the mymodule gradle file:
dependencies {
provided files('libs/android.jar')
}
My project still compiles correctly and I can see v23 files! However, when I run my unit tests, I see the following error:
!!! JUnit version 3.8 or later expected:
java.lang.RuntimeException: Stub!
at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
at junit.textui.TestRunner.<init>(TestRunner.java:54)
at junit.textui.TestRunner.<init>(TestRunner.java:48)
at junit.textui.TestRunner.<init>(TestRunner.java:41)
at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:205)
at com.intellij.rt.execution.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:188)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
It shouldn't matter that the Android JAR is stubbed out because I never execute Android code, I only need it for imports.
Does anyone have any ideas about what I'm seeing and how I could fix this?
EDIT 1: More info:
If I remove the Android dependencies from mymodule entirely, and instead put the following into mymodule-compiler.gradle:
dependencies {
testCompile files('libs/android.jar')
...
}
All my tests will pass. This is obviously not a fix though, as 'mymodule' does need the Android dependency.
EDIT 2:
Doing the same test as EDIT 1, but using the netflix provided scope instead of testCompile works too. If I use the provided scope in both projects though, I get the same Stub! exception.
[Worked out together with OP]
The local jar contains additional classes over the maven jar which seem to interfere.
To get it working delete these from the jar (specifically in this case: all junit classes).
Comparison of the two jars (left is maven jar, right is local jar):
I'm new to testing world and even more to Android testing world. While doing research on Robolectric that aids with tests on android one thing confuses me the most. Sometimes on the web I see people using testCompile keyword in dependencies of the gradle build script when referencing Robolectric while others use androidTestCompile. Certainly both can't be valid?
Can somebody explain the difference between the both and which of these should be the one used when using Robolectric?
Simply testCompile is the configuration for unit tests (those located in src/test) and androidTestCompile is used for the test api (that located in src/androidTest). Since you are intending to write unit tests, you should use testCompile.
Update: 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).
To answer your question - Use testCompile for robolectric
why, because robolectric runs on the JVM mocking all the android device behaviour.
testCompile and androidTestCompile are "by convention" android folders which gradle uses while running tasks provided by android plugin.
androidTestDebug picks tests from androidTest folder,
testDebug picks tests from test folder,
Again these are only by convention folders you can give source sets for these configurations
Note: espresso is such an awesome library try to move away from robolectric :)
//unit testing
testCompile 'junit:junit:4.12'
The above code is a dependency of JUnit 4 in build.gradle file in android studio.
You see that it has testCompile, beacuse JUnit runs on JVM and does not require a device or emulator to run. That also means that JUnit tests will not require the application context to run and if they require we would need to "MOCK" them.
//Insturmented Unit Testing
androidTestCompile('com.android.support.test:runner:0.5', {
exclude group: 'com.android.support', module: 'support-annotations'
})
Now we see androidTestCompile here, because this time we intend to use the device or emulator for tests, that is Instrumentation testing. For beter clarification I would suggest to read from developer.android.com
To add Dependency for JVM testing or Unit testing (testing those rely only on java environment, we don’t need any android environment).
We Use testCompile directive. Example:
dependencies {
testCompile gradleTestKit()
}
To add Dependency for Instrumentation test (Those testing mainly rely on Android environment), we use the androidTestCompile directive.
Apologies for what may seem an idiotic post.
How do you run Mockito on the newest version of Android Studio SDK?
and can you run multiple tests using Mockito using the Android Studio platform?
I've used Mockito on Eclipse and ran as much as 6 tests in the same window. But I'm trying to figure out how to do this on the Android Studio platform and I cannot find any website or tutorial with an answer.
Android Studio 1.1 now has built-in support for unit testing. From Unit testing support - Android Tools Project Site:
Unit tests run on a local JVM on your development machine. Our gradle plugin will compile source code found in src/test/java and execute it using the usual Gradle testing mechanisms. At runtime, tests will be executed against a modified version of android.jar where all final modifiers have been stripped off. This lets you use popular mocking libraries, like Mockito.
You will have to specify your testing dependencies in the build.gradle file of your android module. For example:
dependencies {
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}
The page also contains a step-by-step guide for setting up Android Studio for unit testing, including creating a separate directory for unit tests:
Create a directory for your testing source code, i.e. src/test/java. You can do this from the command line or using the Project view in the Project tool window. The new directory should be highlighted in green at this point. Note: names of the test source directories are determined by the gradle plugin based on a convention.
I'm currently working on a project using junit 4.12 and Mockito 2.0.5 beta for unit testing in Android Studio 1.1, and haven't had any issues:
dependencies {
// ...
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:2.0.5-beta"
}
As far as running multiple tests at the same time, do you mean test cases? Test classes? Test suites? Please clarify, and I'll update my answer, if needed.
Open your app/build.gradle file in your application and add mockito to the dependencies, if dependencies isn't there you can go ahead and create it.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile 'org.mockito:mockito-core:1.10.8'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.1'
}
Then in your unit tests, just create a mock object as you normally would:
http://site.mockito.org/#how
Unit tests should be under the app/src/androidTest/ folder.
I can verify that the accepted answer is correct however just to further to the answer, there will be an androidTest folder alongside your main folder. Normally you would use the androidTest folder for instrumentation tests. Just make sure that under the build variants panel, the Test Artifact: is selected to be "Unit Tests" otherwise the testCompile in build.gradle will not work. It took me a while to figure this part of out.
Hope it helps.