This error has been faced a lot before and asked here. According to the answers, i had to put my instrumentation tests under androidTest folder. So after that, in my case, am still unable to import the AndroidJunit4 class and the pop-up suggestion dialog has now a new option : Setup JDK wich i am surprised with. Anyway i have followed the option and chosen the proper JDK but the problem is still there and the dialog still suggests the same option.
Does anyone know what is the problem here ?
EDIT : My gradle dependencies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:support-annotations:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.parse.bolts:bolts-tasks:1.3.0'
compile 'com.parse:parse-android:1.13.1'
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support:support-annotations:23.4.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
}
I was banging my head against the wall and/or any object i see in front of me in order to make my SQLite function unit tested. No matter what I do, how strictly follow the suggestions provided my many wise people all over the internet did not work.
I then had to go to preschool and start over to realize my stupid mistake. I learned that AndroidJUnit4 can only be used with Instrumentation Test and JUnit must be used for local tests. That being said, the folder must be src/androidTest/java. I had my test class directly under androidTest folder, hence I had to face that nasty error. However, the moment I moved it under src/androidTest/java everything went very clear like "I can see clearly now the rain is gone".
Take a look at this article which says...
Run Instrumented Unit Tests To run your instrumented tests, follow
these steps:
Be sure your project is synchronized with Gradle by clicking Sync
Project in the toolbar. Run your test in one of the following ways:
To run a single test, open the Project window, and then right-click a
test and click Run . To test all methods in a class, right-click a
class or method in the test file and click Run . To run all tests in a
directory, right-click on the directory and select Run tests . The
Android Plugin for Gradle compiles the instrumented test code located
in the default directory (src/androidTest/java/), builds a test APK
and production APK, installs both APKs on the connected device or
emulator, and runs the tests. Android Studio then displays the results
of the instrumented test execution in the Run window.
Therefore folks, for instrumentation test the folder must be (do not forget the case)
src/androidTest/java
and for local tests the folder must be
src/test/java
You can then have your package folder(s) to match your app package
Hope, this helps for the community!
Related
I'am a beginner in AndroidStudio. My first hello world app is stucked in gradle resolving dependencies (app:_debugAndroidTestApk)... it tooks a lot of time doing that so I want to know what is doing.
Also I found in build.gradle that there are two lines I do not know what they are for:
androidTestCompile ...
testCompile ...
Can someone explain in a very basic way what this is all about?
testCompile is the configuration for unit tests, which test data and behavior (located in src/test)
androidTestCompile is used to run tests on your API and any instrumentation tests (located in src/androidTest). These tests run on a physical device or emulator.
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'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.
I have began using Android Studio and gradle recently for android development and find it much better overall than eclipse/ant or maven. However I've recently began trying to implement some kind of unit and or integration tests with my app. I was able to get basic tests working using the Espresso framework recently released by google. I had some tests though where I needed to mock and inject mocked versions of objects. I used dagger in the past for another project so I included dagger into my project. However now my tests won't run because of the following error:
gradle connectedCheck
...
4.1.2 failed: Instrumentation run failed due to 'java.lang.IllegalAccessError' :EspressoApp:connectedCheck
I created a simple demo of this here:
https://github.com/mwolfe38/android-espresso-dagger
Just clone and then from command line run: gradle connectedCheck
In the above I have tried the dependencies several different ways, originally like this:
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.squareup.dagger:dagger-compiler:1.1.0'
compile 'com.squareup.dagger:dagger:1.1.0'
instrumentTestCompile files('libs/espresso-1.0-SNAPSHOT.jar',
'libs/testrunner-1.0-SNAPSHOT.jar',
'libs/testrunner-runtime-1.0-SNAPSHOT.jar')
instrumentTestCompile 'org.hamcrest:hamcrest-all:1.3'
instrumentTestCompile 'com.google.guava:guava:15.0'
}
but that gives me an error regarding static initialization. This appears to be caused by some static initialization code in the espresso framework regarding dagger. So After adding dagger dependencies to instrumentTestCompile I get the IllegalAccessError mentioned above.
Anyone have luck including dagger in your project and doing espresso tests?
Took quite awhile but I finally got it working. I had to do the following:
Declare my dependencies like so:
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.squareup.dagger:dagger-compiler:1.1.0'
compile 'com.squareup.dagger:dagger:1.1.0'
instrumentTestCompile files('libs/espresso-1.0-SNAPSHOT.jar','libs/testrunner-1.0-SNAPSHOT.jar','libs/testrunner-runtime-1.0-SNAPSHOT.jar')
instrumentTestCompile files('libs/hamcrest-core-1.1.jar', 'libs/hamcrest-library-1.1.jar', 'libs/hamcrest-integration-1.1.jar')
instrumentTestCompile 'com.google.guava:guava:14.0.1'
}
Copy the hamcrest jars from here
Remove the license files from the jars like this (or else you'll get an error about duplicate LICENSE.txt files)
zip -d hamcrest-core-1.1.jar LICENSE.txt
zip -d hamcrest-library-1.1.jar LICENSE.txt
Run gradle connectedCheck
A few things to note:
- Hamcrest 1.3 didn't work for me, got an error about a matcher was missing
- Crazy how many hoops I had to jump through to get here.
- Good luck getting this to play well with android studio.
Ok, so I have been dealing with this problem for hours, and here is my fix:
Put this in the dependencies of your build.gradle
compile(project(':commons:statemachine')) {
exclude module: 'junit'
exclude module: 'json'
exclude module: 'guava'
}
compile 'com.google.guava:guava:15.0'
instrumentTestCompile files('libs/espresso-1.0-SNAPSHOT-bundled.jar')
instrumentTestCompile 'com.squareup:fest-android:1.0.+'
Add the espresso bundled jar in the libs folder of your test. Now comes the important part.
Open that espresso bundled jar with WinRar or equivalent and go to com/ folder, then select de android folder and Delete it. Now close WinRar and compile and run your test :-)