How to use Mockito/Hamcrest in unit tests in Android Studio -


i want able make unit tests , instrumentation tests in android studio, , using mockito in them.

i'm using new approach tests in android studio 0.8. is:

  • building gradle
  • using official android api testing (activityinstrumentationtestcase2, etc)
  • having tests inside directory of app, not separate module
  • launching tests in android studio "android test" run configuration

how can write code in tests depends on libraries used tests, such mockito or hamcrest?

i'd include these libraries when compiling , running tests, avoid them exported released .apk.

in https://code.google.com/p/mockito/wiki/declaringmockitodependency i've read should add dependency as:

dependencies {     ....     testcompile "org.mockito:mockito-core:1.9.5" } 

but when running get:

build script error, unsupported gradle dsl method found: 'testcompile()'!

although i'm not sure it's relevant, gradle build file i'm using is:

apply plugin: 'android'  dependencies {     compile filetree(dir: 'libs', include: '*.jar')     compile project(':android-sdk')     testcompile "org.mockito:mockito-core:1.9.5" }  android {     compilesdkversion 19     buildtoolsversion "20.0.0"      compileoptions {         sourcecompatibility javaversion.version_1_7         targetcompatibility javaversion.version_1_7     }      sourcesets {         main {             manifest.srcfile 'androidmanifest.xml'             java.srcdirs = ['src']             resources.srcdirs = ['src']             aidl.srcdirs = ['src']             renderscript.srcdirs = ['src']             res.srcdirs = ['res']             assets.srcdirs = ['assets']         }          // move tests tests/java, tests/res, etc...         androidtest.setroot('tests')          // note - run tests command line:         // $ gradle clean connectedcheck build         // (requires gradle 1.10)          // move build types build-types/<type>         // instance, build-types/debug/java, build-types/debug/androidmanifest.xml, ...         // moves them out of them default location under src/<type>/...         // conflict src/ being used main source set.         // adding new build types or product flavors should accompanied         // similar customization.         debug.setroot('build-types/debug')         release.setroot('build-types/release')     } } 

i got working using "androidtestcompile" options under "dependencies", explained here.

what have done is:

  • created folder called libs-tests jars should used testing.
  • added folder dependency tests "androidtestcompile"

now, gradle build file stands as:

apply plugin: 'android'  dependencies {     compile project(':android-sdk')      // libs folder included in apk of real app     compile filetree(dir: 'libs', include: '*.jar')      // tests-libs folder included tests     androidtestcompile filetree(dir: 'libs-tests', include: '*.jar') }   android {     compilesdkversion 19     buildtoolsversion "20.0.0"      compileoptions {         sourcecompatibility javaversion.version_1_7         targetcompatibility javaversion.version_1_7     }       sourcesets {         main {             manifest.srcfile 'androidmanifest.xml'             java.srcdirs = ['src']             resources.srcdirs = ['src']             aidl.srcdirs = ['src']             renderscript.srcdirs = ['src']             res.srcdirs = ['res']             assets.srcdirs = ['assets']         }          // move tests tests/java, tests/res, etc...         androidtest.setroot('tests')          // note - run tests command line:         // $ gradle clean connectedcheck build         // (requires gradle 1.10)          // move build types build-types/<type>         // instance, build-types/debug/java, build-types/debug/androidmanifest.xml, ...         // moves them out of them default location under src/<type>/...         // conflict src/ being used main source set.         // adding new build types or product flavors should accompanied         // similar customization.         debug.setroot('build-types/debug')         release.setroot('build-types/release')     } } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -