Peter3514608
Peter3514608

Reputation: 21

Testing using Mockito

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.

Upvotes: 0

Views: 7554

Answers (3)

Pirdad Sakhizada
Pirdad Sakhizada

Reputation: 608

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.

Upvotes: 0

unrulygnu
unrulygnu

Reputation: 1596

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:

  1. 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.

Upvotes: 1

James
James

Reputation: 3565

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.

Upvotes: 0

Related Questions