These days I have been using Android Studio v1.0 and I want to do test using Robolectric, which give you more freedom for unit testing in Android. You can perform your test without emulator running. But I have faced setting problems even though the website looks like they're explaining well about how to setup the configuration. I blamed my brain but I found out there were missings for Android Studio gradle build and I had to google it a little bit more.
I am not familiar with Gradle yet, but Android Studio uses Gradle as default build tool and Gradle seems to be a quite great build tool.
There are two 'build.gradle's for Android Studio project. These are the main file for build configuration. One of them(a) is in your project folder is for global setting and the other one(b) in the 'app' folder is just for your app.
myproject - build.gradle (a) - app - build.gradle (b)
For the right setup, we need to touch a few things.
1) Installation 'Android Studio Unit Test' plugin.
2) Setting for JC&K Gradle plugin(depricated since Android Gradle version 1.1)
3) Dependencies configuration
4) Open the "Build variants" tool window (on the left) and change the test artifact to "Unit tests".
5) Write unit test using JUnit and RobolectricTestRunner
2) Setting for JC&K Gradle plugin Open myprject/build.gradle. Add 'classpath 'com.github.jcandksolutions.gradle:android-unit-test:2.1.1' in dependency section. Now, myproject/build.gradle looks like the following:
Since version 1.1 of the Android Gradle plugin, unit tests in JVM are natively supported. This plugin has been deprecated from https://github.com/JCAndKSolutions/android-unit-test#deprecation-notice
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.0.0'classpath 'com.github.jcandksolutions.gradle:android-unit-test:2.1.1'} } allprojects { repositories { jcenter() } }
And then, we need to apply the Gradle plugin in our app’s build.gradle file. Add 'apply plugin 'android-unit-test' after buildType and dependencies section.
Since version 1.1 of the Android Gradle plugin, you don't nee android-unit-test
plugin any more.
apply plugin: 'com.android.application' android {...}apply plugin: 'android-unit-test'dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) }
3) Dependencies configuration Now, we need to setup dependencies. Roboreltric uses JUnit4 as a test framework, so we need JUnit , Robolectric as dependencies and other things like below:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.0' testCompile 'junit:junit:4.12' testCompile 'org.mockito:mockito-core:1.9.5' //optional testCompile 'org.robolectric:robolectric:3.0-rc2' testCompile 'com.squareup:fest-android:1.0.8' }
I am using appcompat library to use material design in lower versions and I setup its version to 21.0.0. It is different Android Studio gives you a bit different version when you create API 21 project.
Now, it should look like this:
apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion '21.1.2' defaultConfig { applicationId "your.package.name" minSdkVersion 21 targetSdkVersion 21 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.0' testCompile 'junit:junit:4.12' testCompile 'org.mockito:mockito-core:1.9.5' testCompile 'org.robolectric:robolectric:3.0-rc2' testCompile 'com.squareup:fest-android:1.0.8' }
4) Open the "Build variants" tool window (on the left) and change the test artifact to "Unit tests".
5) Write unit test using JUnit and RobolectricTestRunner
import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.model.InitializationError; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; @Config(emulateSdk = 18) @RunWith(value = RobolectricTestRunner.class) public class MyFristRoboreltricTest { @Test public void testMethod1(){ assertTrue( false) } }
If you give emulateSdk = 21
, it will give error when you run the test. It look like Roboreltric does not support above API 21 yet. So, you have setup emulateSdk for @Config annotation under 21.
your_project/app/src/androidTest
as default. But Robotinum does not recognise the folder. You can change your app build.gradle to set test folder:
sourceSets { androidTest { setRoot('src/androidTest') } }
But unfortunately, it did not work. Weirdly, when I change the test folder androidTest
to test
and commented the sourceSets section, it worked charm. I haven't looked into Android Studio Gradle source yet, but I can guess test path may be hardcoded.
More links:
http://stackoverflow.com/questions/29714750/android-studio-1-1-0-cannot-setup-robolectric
http://tools.android.com/tech-docs/unit-testing-support
This has been deprecated.
ReplyDelete