How to setup unit testing in Android Studio

Click here to load reader

  • date post

    19-Aug-2014
  • Category

    Education

  • view

    9.138
  • download

    1

Embed Size (px)

description

Using Robolectric and jUnit with Android Studio and Gradle

Transcript of How to setup unit testing in Android Studio

  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 http://www.flickr.com/photos/johnhaydon/5161176888/ How to setup unit testing in Android Studio Testing
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 What changed at the slides Changelog v.1.0.1 Removed duplicate slide v.1.0.0 Initial version
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 A tiny piece of structure The presentation Part 1: General project setup Part 2: IDE integration
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Part 1: General project setup The easy part
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Expecting the default project structure of Android Studio / Gradle Project structure build.gradle app build.gradle src main AndroidManifest.xml java res
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Robolectric and jUnit 4 to app/build.gradle Dependencies dependencies { androidTestCompile 'org.robolectric:robolectric:2.+' androidTestCompile 'junit:junit:4.+' }
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Add novoda/robolectric-plugin to /build.gradle Gradle plugin buildscript { repositories { mavenCentral() // Add this repository: maven { url 'https://oss.sonatype.org/content/repositories/snapshots' } } dependencies { classpath 'com.android.tools.build:gradle:0.8.+' // And this dependency: classpath 'com.novoda.gradle:robolectric-plugin:0.0.1-SNAPSHOT' } } allprojects { repositories { mavenCentral() // And finally this repository again. maven { url 'https://oss.sonatype.org/content/repositories/snapshots' } } }
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Apply the plugin to app/build.gradle Gradle plugin apply plugin: 'robolectric' Now sync ...
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Create a test folder: app/src/test/java/com/example/app/test Create a test package com.example.app.test; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import static org.junit.Assert.*; @RunWith(RobolectricTestRunner.class) public class RoboTest { @Test public void testTrueIsTrue() throws Exception { assertEquals(true, true); } }
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Android Studio does not recognize app/src/test/java as source directory Run the test (from the shell) $ ./gradlew robolectric Should look like this ... :app:assemble :app:compileRobolectricJava :app:processRobolectricResources UP-TO-DATE :app:robolectricClasses :app:robolectric BUILD SUCCESSFUL
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 The confusing part Part 2: IDE integration
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Add a task to app/build.gradle Register the test folder task addTest { description "Registers the test folder in the .iml file so Java tooling works." def src = ['src/test/java'] def file = file("app.iml") // Must match module file name doLast { try { def parsedXml = (new XmlParser()).parse(file) def node = parsedXml.component[1].content[0] src.each { def path = 'file://$MODULE_DIR$/' + "${it}" def set = node.find { [email protected] == path } if (set == null) { new Node(node, 'sourceFolder', ['url': 'file://$MODULE_DIR$/' + "${it}", 'isTestSource': "true"]) def writer = new StringWriter() new XmlNodePrinter(new PrintWriter(writer)).print(parsedXml) file.text = writer.toString() } } } catch (FileNotFoundException e) { // nop, iml not found } } } // always do the addtest on prebuild gradle.projectsEvaluated { preBuild.dependsOn(addTest) }
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 The test folder is recognized by Android Studio Register the test folder
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Pick it from the context menu ... Run All Tests
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Test were not started Failed to run tests !!! JUnit version 3.8 or later expected: java.lang.RuntimeException: Stub! at junit.runner.BaseTestRunner.(BaseTestRunner.java:5) at junit.textui.TestRunner.(TestRunner.java:54) at junit.textui.TestRunner.(TestRunner.java:48) at junit.textui.TestRunner.(TestRunner.java:41) Process finished with exit code 253 Copy everything printed to the output!
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Before ... Some text editor work /usr/lib/jvm/jdk1.7.0/bin/java -ea -Didea.launcher.port=7532 -Didea.launcher.bin.path=/opt/android-studio/bin -Dfile.encoding=UTF-8 -classpath /opt/android-studio/lib/idea_rt.jar:/opt/android-studio/plugins/junit/lib/junit-rt.jar:/media/data/SDKs/android-sdk-linux_x86/platforms/android-19/android.jar:/ media/data/SDKs/android-sdk-linux_x86/platforms/android-19/data/res:/media/data/SDKs/android-sdk-linux_x86/tools/support/annotations.jar:/media/data/Development _Ubuntu/TestRobolectricHome/app/build/classes/debug:/home/tobias/.gradle/caches/modules-2/files-2.1/classworlds/classworlds/1.1-alpha-2/5adf2e681c57d7f48038b602 f3ca2254ee82d47/classworlds-1.1-alpha-2.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.codehaus.plexus/plexus-utils/1.5.15/c689598ce1eb94c304817877ed15 911099972526/plexus-utils-1.5.15.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.objenesis/objenesis/1.3/dc13ae4faca6df981fc7aeb5a522d9db446d5d50/objene sis-1.3.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-artifact-manager/2.2.1/ec355b913c34d37080810f98e3f51abecbe1572b/maven-artifac t-manager-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/nekohtml/nekohtml/1.9.6.2/2d960be7b62ae6622dbbbe49ab4ffdc609f85c80/nekohtml-1.9.6.2.jar:/hom e/tobias/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-tree/4.1/51085abcc4cb6c6e1cb5551e6f999eb8e31c5b2d/asm-tree-4.1.jar:/home/tobias/.gradle/caches/modul es-2/files-2.1/org.ow2.asm/asm/4.1/ad568238ee36a820bd6c6806807e8a14ea34684d/asm-4.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.xerial/sqlite-jdbc/3 .7.2/7a3d67f00508d3881650579f7f228c61bfc1b196/sqlite-jdbc-3.7.2.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.ant/ant/1.8.0/7b456ca6b93900f96e5 8cc8371f03d90a9c1c8d1/ant-1.8.0.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-repository-metadata/2.2.1/98f0c07fcf1eeb213bef8d9316a 9935184084b06/maven-repository-metadata-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-ant-tasks/2.1.3/b09be554228d66d208e5fef 5266844aacf443abc/maven-ant-tasks-2.1.3.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-error-diagnostics/2.2.1/e81bb342d7d172f23d108 dc8fa979a1facdcde8e/maven-error-diagnostics-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven.wagon/wagon-http-lightweight/1.0-beta-6/b 3815078570c3b1f0667e123d59717c6b726c6c2/wagon-http-lightweight-1.0-beta-6.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/commons-codec/commons-codec/1.3/fd 32786786e2adb664d5ecc965da47629dca14ba/commons-codec-1.3.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven.wagon/wagon-http-shared/1.0-beta-6 /ccd70d7e0d8c085e648a83f072da06ca9a53be94/wagon-http-shared-1.0-beta-6.jar:/media/data/Development_Ubuntu/TestRobolectricHome/app/build/exploded-aar/com.android .support/appcompat-v7/19.0.1/res:/media/data/Development_Ubuntu/TestRobolectricHome/app/build/exploded-aar/com.android.support/appcompat-v7/19.0.1/classes.jar:/ home/tobias/.gradle/caches/modules-2/files-2.1/org.robolectric/robolectric/2.2/af902024b55e3b41732a7d9f41c32f90c065be2f/robolectric-2.2.jar:/home/tobias/.gradle /caches/modules-2/files-2.1/org.codehaus.plexus/plexus-container-default/1.0-alpha-9-stable-1/94aea3010e250a334d9dab7f591114cd6c767458/plexus-container-default- 1.0-alpha-9-stable-1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/backport-util-concurrent/backport-util-concurrent/3.1/682f7ac17fed79e92f8e87d8455192b63 376347b/backport-util-concurrent-3.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-plugin-registry/2.2.1/72a24b7775649af78f3986b5aa 7eb354b9674cfd/maven-plugin-registry-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-settings/2.2.1/2236ffe71fa5f78ce42b0f5fc22 c54ed45f14294/maven-settings-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven.wagon/wagon-file/1.0-beta-6/6c53633505460caf49d2660de1e2 4744d915afb9/wagon-file-1.0-beta-6.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/com.ximpleware/vtd-xml/2.11/ee5bcf62c1acf76434ee9f1c67a840bafef72a6d/vtd- xml-2.11.jar:/media/data/SDKs/android-sdk-linux_x86/extras/android/m2repository/com/android/support/support-v4/19.0.1/support-v4-19.0.1.jar:/home/tobias/.gradle /caches/modules-2/files-2.1/org.ow2.asm/asm-commons/4.1/f8b86f4ee6e02082f63a658e00eb5506821253c6/asm-commons-4.1.jar:/home/tobias/.gradle/caches/modules-2/files -2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.ow2.asm/a sm-analysis/4.1/73401033069e4714f57b60aeae02f97210aaa64e/asm-analysis-4.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/junit/junit/4.11/4e031bb61df09069a eb2bffb4019e7a5034a4ee0/junit-4.11.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-artifact/2.2.1/23600f790d4dab2cb965419eaa982e3e84c 428f8/maven-artifact-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-profile/2.2.1/3950071587027e5086e9c395574a60650c432738/mav en-profile-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/com.google.android/support-v4/r7/24d0f6da34c3a2bfcf736ab42d51c91ac821ee22/support-v4-r7.jar :/home/tobias/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-util/4.1/6344065cb0f94e2b930a95e6656e040ebc11df08/asm-util-4.1.jar:/home/tobias/.gradle/caches/ modules-2/files-2.1/org.apache.maven/maven-model/2.2.1/c0a1c17436ec3ff5a56207c031d82277b4250a29/maven-model-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/file s-2.1/org.easytesting/fest-util/1.2.5/c4a8d7305b23b8d043be12c979813b096df11f44/fest-util-1.2.5.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.an t/ant-launcher/1.8.0/8b53ba16fa62fb1034da8f1de200ddc407c8381/ant-launcher-1.8.0.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.codehaus.plexus/plexus-i nterpolation/1.11/ad9dddff6043194904ad1d2c00ff1d003c3915f7/plexus-interpolation-1.11.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.easytesting/fest-re flect/1.4.1/2b92d5275e92a49e16c7ce6bd7e46b9080db0530/fest-reflect-1.4.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-project/2.2.1 /8239e98c16f641d55a4ad0e0bab0aee3aff8933f/maven-project-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven.wagon/wagon-provider-api/1.0- beta-6/3f952e0282ae77ae59851d96bb18015e520b6208/wagon-provider-api-1.0-beta-6.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/nekohtml/xercesMinimal/1.9.6.2 /d1c5e063683a0e6f77cd5f051a9d4af48346fa6/xercesMinimal-1.9.6.2.jar ... Remove everything before -classpath Find juni part and prepend it
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 After ... Some text editor work -classpath /home/tobias/.gradle/caches/modules-2/files-2.1/junit/junit/4.11/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar:/opt/android-studio/lib/idea_rt.jar:/op t/android-studio/plugins/junit/lib/junit-rt.jar:/media/data/SDKs/android-sdk-linux_x86/platforms/android-19/android.jar:/media/data/SDKs/android-sdk-linux_x86/p latforms/android-19/data/res:/media/data/SDKs/android-sdk-linux_x86/tools/support/annotations.jar:/media/data/Development_Ubuntu/TestRobolectricHome/app/build/c lasses/debug:/home/tobias/.gradle/caches/modules-2/files-2.1/classworlds/classworlds/1.1-alpha-2/5adf2e681c57d7f48038b602f3ca2254ee82d47/classworlds-1.1-alpha-2 .jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.codehaus.plexus/plexus-utils/1.5.15/c689598ce1eb94c304817877ed15911099972526/plexus-utils-1.5.15.jar:/h ome/tobias/.gradle/caches/modules-2/files-2.1/org.objenesis/objenesis/1.3/dc13ae4faca6df981fc7aeb5a522d9db446d5d50/objenesis-1.3.jar:/home/tobias/.gradle/caches /modules-2/files-2.1/org.apache.maven/maven-artifact-manager/2.2.1/ec355b913c34d37080810f98e3f51abecbe1572b/maven-artifact-manager-2.2.1.jar:/home/tobias/.gradl e/caches/modules-2/files-2.1/nekohtml/nekohtml/1.9.6.2/2d960be7b62ae6622dbbbe49ab4ffdc609f85c80/nekohtml-1.9.6.2.jar:/home/tobias/.gradle/caches/modules-2/files -2.1/org.ow2.asm/asm-tree/4.1/51085abcc4cb6c6e1cb5551e6f999eb8e31c5b2d/asm-tree-4.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm/4.1/ad56 8238ee36a820bd6c6806807e8a14ea34684d/asm-4.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.xerial/sqlite-jdbc/3.7.2/7a3d67f00508d3881650579f7f228c61bf c1b196/sqlite-jdbc-3.7.2.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.ant/ant/1.8.0/7b456ca6b93900f96e58cc8371f03d90a9c1c8d1/ant-1.8.0.jar:/ho me/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-repository-metadata/2.2.1/98f0c07fcf1eeb213bef8d9316a9935184084b06/maven-repository-metadata -2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-ant-tasks/2.1.3/b09be554228d66d208e5fef5266844aacf443abc/maven-ant-tasks-2.1.3 .jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-error-diagnostics/2.2.1/e81bb342d7d172f23d108dc8fa979a1facdcde8e/maven-error-diagnos tics-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven.wagon/wagon-http-lightweight/1.0-beta-6/b3815078570c3b1f0667e123d59717c6b726c6c2 /wagon-http-lightweight-1.0-beta-6.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/commons-codec/commons-codec/1.3/fd32786786e2adb664d5ecc965da47629dca14ba/ commons-codec-1.3.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven.wagon/wagon-http-shared/1.0-beta-6/ccd70d7e0d8c085e648a83f072da06ca9a53be 94/wagon-http-shared-1.0-beta-6.jar:/media/data/Development_Ubuntu/TestRobolectricHome/app/build/exploded-aar/com.android.support/appcompat-v7/19.0.1/res:/media /data/Development_Ubuntu/TestRobolectricHome/app/build/exploded-aar/com.android.support/appcompat-v7/19.0.1/classes.jar:/home/tobias/.gradle/caches/modules-2/fi les-2.1/org.robolectric/robolectric/2.2/af902024b55e3b41732a7d9f41c32f90c065be2f/robolectric-2.2.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.codehau s.plexus/plexus-container-default/1.0-alpha-9-stable-1/94aea3010e250a334d9dab7f591114cd6c767458/plexus-container-default-1.0-alpha-9-stable-1.jar:/home/tobias/. gradle/caches/modules-2/files-2.1/backport-util-concurrent/backport-util-concurrent/3.1/682f7ac17fed79e92f8e87d8455192b63376347b/backport-util-concurrent-3.1.ja r:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-plugin-registry/2.2.1/72a24b7775649af78f3986b5aa7eb354b9674cfd/maven-plugin-registry-2. 2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-settings/2.2.1/2236ffe71fa5f78ce42b0f5fc22c54ed45f14294/maven-settings-2.2.1.jar: /home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven.wagon/wagon-file/1.0-beta-6/6c53633505460caf49d2660de1e24744d915afb9/wagon-file-1.0-beta-6.jar: /home/tobias/.gradle/caches/modules-2/files-2.1/com.ximpleware/vtd-xml/2.11/ee5bcf62c1acf76434ee9f1c67a840bafef72a6d/vtd-xml-2.11.jar:/media/data/SDKs/android-s dk-linux_x86/extras/android/m2repository/com/android/support/support-v4/19.0.1/support-v4-19.0.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.ow2.asm /asm-commons/4.1/f8b86f4ee6e02082f63a658e00eb5506821253c6/asm-commons-4.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a 25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-analysis/4.1/73401033069e4714f57b60a eae02f97210aaa64e/asm-analysis-4.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-artifact/2.2.1/23600f790d4dab2cb965419eaa982e3e84c 428f8/maven-artifact-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-profile/2.2.1/3950071587027e5086e9c395574a60650c432738/mav en-profile-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/com.google.android/support-v4/r7/24d0f6da34c3a2bfcf736ab42d51c91ac821ee22/support-v4-r7.jar :/home/tobias/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-util/4.1/6344065cb0f94e2b930a95e6656e040ebc11df08/asm-util-4.1.jar:/home/tobias/.gradle/caches/ modules-2/files-2.1/org.apache.maven/maven-model/2.2.1/c0a1c17436ec3ff5a56207c031d82277b4250a29/maven-model-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/file s-2.1/org.easytesting/fest-util/1.2.5/c4a8d7305b23b8d043be12c979813b096df11f44/fest-util-1.2.5.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.an t/ant-launcher/1.8.0/8b53ba16fa62fb1034da8f1de200ddc407c8381/ant-launcher-1.8.0.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.codehaus.plexus/plexus-i nterpolation/1.11/ad9dddff6043194904ad1d2c00ff1d003c3915f7/plexus-interpolation-1.11.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.easytesting/fest-re flect/1.4.1/2b92d5275e92a49e16c7ce6bd7e46b9080db0530/fest-reflect-1.4.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven/maven-project/2.2.1 /8239e98c16f641d55a4ad0e0bab0aee3aff8933f/maven-project-2.2.1.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/org.apache.maven.wagon/wagon-provider-api/1.0- beta-6/3f952e0282ae77ae59851d96bb18015e520b6208/wagon-provider-api-1.0-beta-6.jar:/home/tobias/.gradle/caches/modules-2/files-2.1/nekohtml/xercesMinimal/1.9.6.2 /d1c5e063683a0e6f77cd5f051a9d4af48346fa6/xercesMinimal-1.9.6.2.jar ... The juni part is at the beginning now
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Classpath goes into VM options Setup run configuration
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Create a Gradle task Generate Robolectric classes
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Add to JUnit configuration, Before launch Launch Robolectric configuration
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 Big thanks to Ilkka Laukkanen Credits Further information: http://blog.futurice.com/android_unit_testing_in_ides_and_ci_environments
  • Tobias Preuss 26.03.2014 GDG Android in Berlin v.1.0.1 http://www.flickr.com/photos/dopey/123646856/ Questions