1*e074118cSAndroid Build Coastguard Worker# Mockito-Kotlin 2*e074118cSAndroid Build Coastguard Worker[  ](https://maven-badges.herokuapp.com/maven-central/org.mockito.kotlin/mockito-kotlin) 3*e074118cSAndroid Build Coastguard Worker 4*e074118cSAndroid Build Coastguard WorkerA small library that provides helper functions to work with [Mockito](https://github.com/mockito/mockito) in Kotlin. 5*e074118cSAndroid Build Coastguard Worker 6*e074118cSAndroid Build Coastguard Worker## Install 7*e074118cSAndroid Build Coastguard Worker 8*e074118cSAndroid Build Coastguard WorkerMockito-Kotlin is available on Maven Central and JCenter. 9*e074118cSAndroid Build Coastguard WorkerFor Gradle users, add the following to your `build.gradle`, replacing `x.x.x` with the latest version: 10*e074118cSAndroid Build Coastguard Worker 11*e074118cSAndroid Build Coastguard Worker```groovy 12*e074118cSAndroid Build Coastguard WorkertestImplementation "org.mockito.kotlin:mockito-kotlin:x.x.x" 13*e074118cSAndroid Build Coastguard Worker``` 14*e074118cSAndroid Build Coastguard Worker 15*e074118cSAndroid Build Coastguard Worker## Example 16*e074118cSAndroid Build Coastguard Worker 17*e074118cSAndroid Build Coastguard WorkerA test using Mockito-Kotlin typically looks like the following: 18*e074118cSAndroid Build Coastguard Worker 19*e074118cSAndroid Build Coastguard Worker```kotlin 20*e074118cSAndroid Build Coastguard Worker@Test 21*e074118cSAndroid Build Coastguard Workerfun doAction_doesSomething(){ 22*e074118cSAndroid Build Coastguard Worker /* Given */ 23*e074118cSAndroid Build Coastguard Worker val mock = mock<MyClass> { 24*e074118cSAndroid Build Coastguard Worker on { getText() } doReturn "text" 25*e074118cSAndroid Build Coastguard Worker } 26*e074118cSAndroid Build Coastguard Worker val classUnderTest = ClassUnderTest(mock) 27*e074118cSAndroid Build Coastguard Worker 28*e074118cSAndroid Build Coastguard Worker /* When */ 29*e074118cSAndroid Build Coastguard Worker classUnderTest.doAction() 30*e074118cSAndroid Build Coastguard Worker 31*e074118cSAndroid Build Coastguard Worker /* Then */ 32*e074118cSAndroid Build Coastguard Worker verify(mock).doSomething(any()) 33*e074118cSAndroid Build Coastguard Worker} 34*e074118cSAndroid Build Coastguard Worker``` 35*e074118cSAndroid Build Coastguard Worker 36*e074118cSAndroid Build Coastguard WorkerFor more info and samples, see the [Wiki](https://github.com/mockito/mockito-kotlin/wiki). 37*e074118cSAndroid Build Coastguard Worker 38*e074118cSAndroid Build Coastguard Worker## Building 39*e074118cSAndroid Build Coastguard Worker 40*e074118cSAndroid Build Coastguard WorkerMockito-Kotlin is built with Gradle. 41*e074118cSAndroid Build Coastguard Worker 42*e074118cSAndroid Build Coastguard Worker - `./gradlew build` builds the project 43*e074118cSAndroid Build Coastguard Worker - `./gradlew publishToMavenLocal` installs the maven artifacts in your local repository 44*e074118cSAndroid Build Coastguard Worker - `./gradlew assemble && ./gradlew test` runs the test suite (See Testing below) 45*e074118cSAndroid Build Coastguard Worker 46*e074118cSAndroid Build Coastguard Worker### Versioning 47*e074118cSAndroid Build Coastguard Worker 48*e074118cSAndroid Build Coastguard WorkerMockito-Kotlin roughly follows SEMVER; version names are parsed from 49*e074118cSAndroid Build Coastguard Workergit tags using `git describe`. 50*e074118cSAndroid Build Coastguard Worker 51*e074118cSAndroid Build Coastguard Worker### Testing 52*e074118cSAndroid Build Coastguard Worker 53*e074118cSAndroid Build Coastguard WorkerMockito-Kotlin's test suite is located in a separate `tests` module, 54*e074118cSAndroid Build Coastguard Workerto allow running the tests using several Kotlin versions whilst still 55*e074118cSAndroid Build Coastguard Workerkeeping the base module at a recent version. 56*e074118cSAndroid Build Coastguard Worker 57*e074118cSAndroid Build Coastguard WorkerTesting thus must be done in two stages: one to build the base artifact 58*e074118cSAndroid Build Coastguard Workerto test against, and the actual execution of the tests against the 59*e074118cSAndroid Build Coastguard Workerbuilt artifact: 60*e074118cSAndroid Build Coastguard Worker 61*e074118cSAndroid Build Coastguard Worker - `./gradlew assemble` builds the base artifact 62*e074118cSAndroid Build Coastguard Worker - `./gradlew test` runs the tests against the built artifact. 63*e074118cSAndroid Build Coastguard Worker 64*e074118cSAndroid Build Coastguard WorkerUsually it is enough to test only using the default Kotlin versions; 65*e074118cSAndroid Build Coastguard WorkerCI will test against multiple versions. 66*e074118cSAndroid Build Coastguard WorkerIf you want to test using a different Kotlin version locally, set 67*e074118cSAndroid Build Coastguard Workeran environment variable `KOTLIN_VERSION` and run the tests. 68*e074118cSAndroid Build Coastguard Worker 69*e074118cSAndroid Build Coastguard Worker### Acknowledgements 70*e074118cSAndroid Build Coastguard Worker 71*e074118cSAndroid Build Coastguard Worker`mockito-kotlin` was created and developed by [nhaarman@](https://github.com/nhaarman) after which the repository was integrated into the official Mockito GitHub organization. 72*e074118cSAndroid Build Coastguard WorkerWe would like to thank Niek for the original idea and extensive work plus support that went into `mockito-kotlin`. 73