Reputation: 6579
Hitherto I used robolectric unit testing with JUnit 4.x for testing my business logic. In the last version of Android Studio 1.1.0 was announced native support of unit testing junit:4.+
.
Should I refuse using robolectric? Has robolectric some distinct advantages that I might not know?
As for me using Android Studio native tests more convenient and simpler. In the robolectric test results are stored in html file and can be displayed in the browser(which not convinient for me). Native Android Studio test results are displayed in the Run output window, if some test fails, we can easily open this line of code by clicking on the error in the output window.
Upvotes: 3
Views: 1836
Reputation: 86
There is no reason you cannot use both. Robolectric has the advantage that you can test against actual Android behaviour when needed. Plain JUnit will just let you test the parts of your code that do not interact with Android.
This will let you view the results of the robolectric tests in Android Studio along with the plain JUnit 4.x tests
If you are using gradle, the robolectric example github repo has an example of how to do it.
I'm currently migrating a few apps to use this approach
Here are the relevant parts of the build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-core:1.1'
testCompile 'org.hamcrest:hamcrest-library:1.1'
testCompile 'org.hamcrest:hamcrest-integration:1.1'
testCompile('org.robolectric:robolectric:2.4') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
exclude module: 'httpclient'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-provider-api'
}
}
The test classes shouldn't need any modification
Upvotes: 2