Step #3: Adding Some Unit Test Dependencies
So far, all of the dependencies that we have been adding to our app have used the implementation
keyword. Those dependencies become part of the main app.
However, our dependencies
closure in app/build.gradle
also has androidTestImplementation
and testImplementation
statements. These are for instrumented tests and unit tests, respectively:
Test Type | Where the Source Goes | How You Add Dependencies |
---|---|---|
instrumented test | androidTest |
androidTestImplementation |
unit test | test |
testImplementation |
Right now, we have just one testImplementation
dependency, for JUnit. JUnit is the foundation of all Android unit tests and instrumented tests, so we will be writing JUnit-based tests for both types.
Technically, we do not need anything more than that for our unit tests. In practice, though, usually we add some more dependencies, ones that will help us test more effectively.
With that in mind, add these lines to the dependencies
closure in app/build.gradle
:
testImplementation "org.mockito:mockito-inline:3.12.1"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.1'
The org.mockito:mockito-inline
and com.nhaarman.mockitokotlin2:mockito-kotlin
bring in Mockito and a Kotlin wrapper for Mockito. Mockito is a popular “mocking” library, allowing us to define ad hoc implementations of classes. Sometimes, we use these to test scenarios that would be difficult to test otherwise. Sometimes, we use these as “call recorders”, to see whether certain functions were called as part of our tests.
The org.jetbrains.kotlinx:kotlinx-coroutines-test
library helps you test coroutines. The author hopes that this was not a surprise.
(if it was… surprise!)
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.