Skip to content
This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
/ mockito4k Public archive

[DEPRECATED] Please use MockK or Mockito-Kotlin.

License

Notifications You must be signed in to change notification settings

tmurakami/mockito4k

Repository files navigation

DEPRECATED

This library is deprecated. You can use MockK or Mockito-Kotlin.

Mockito4k

CircleCI Release KDoc
Kotlin Mockito

A Kotlin wrapper around Mockito 2.

Mock creation

Use mock function.

val mock = mock<Foo>()

To create a mock with additional settings, use mock(MockSettings.() -> Unit) function.

val mock = mock<Foo> { name("foo") }

Stubbing

Use given function.

given(mock) {
    calling { doSomething("foo") }.willReturn("bar")
}

This function can also be used for properties, Unit (void) functions, and spied objects.

given(mock) {
    calling { someProperty = "foo" }.willReturn(Unit) // Same as Mockito#doNothing()
}

You can use willThrow function to make a function throw any checked exceptions without @Throws annotation.

interface Foo {
    fun doSomething(): String
}

class SomeException : Exception()

@Test(expected = SomeException::class)
fun test() {
    given(mock<Foo>()) {
        calling { doSomething() }.willThrow(SomeException())
    }.doSomething()
}

To call default implementations of interface functions, you can use willCallRealMethod function.

interface Foo {
    fun doSomething(): String = "Do something"
}

@Test
fun test() {
    assertEquals("Do something", given(mock<Foo>()) {
        calling { doSomething() }.willCallRealMethod()
    }.doSomething())
}

Verification

Currently this library does not provide any function for verification, so use Mockito's BDDMockito#then(T).

import org.mockito.BDDMockito.then

then(mock).should().doSomething("foo")
then(mock).should().someProperty = "bar"

Comparing arguments

This library provides the following matchers as top-level functions.

  • anyNullable
  • any
  • eq
  • refEq
  • same
  • isA
  • isNull
  • isNotNull
  • nullable
  • matches
  • argThat
  • and
  • or
  • not
  • geq
  • gt
  • leq
  • lt
  • cmpEq
  • find
  • aryEq

Applying a matcher written in Java to a function that does not accept null may throw an IllegalStateException with the message xxx must not be null. To prevent this, use by function as follows:

mock.doSomething(by(MatchersWrittenInJava.matchesSomething()))

Capturing arguments

Use argumentCaptor function.

val captor = argumentCaptor<String>()

Applying ArgumentCaptor#capture() to a function that does not accept null will throw an IllegalStateException with the message xxx.capture() must not be null. To prevent this, use capture function instead.

mock.doSomething(capture(captor))

Installation

First, add the JitPack repository and the JCenter repository to your build.gradle.

repositories {
    maven { url 'https://jitpack.io' }
    jcenter()
}

And then, add this library and mockito-core as testCompile dependency.

dependencies {
    testCompile 'com.github.tmurakami:mockito4k:x.y.z'
    testCompile 'org.mockito:mockito-core:x.y.z' // 2.7.0 or later
}

Release

To use this with mockito-android, add them as androidTestCompile dependency.

dependencies {
    androidTestCompile 'com.github.tmurakami:mockito4k:x.y.z'
    androidTestCompile 'org.mockito:mockito-android:x.y.z' // 2.7.0 or later
}

Limitations

  • Stubbing the following functions does not work.

    • Extension functions: They are compiled to static methods that Mockito cannot stub.
    • Inline functions: They are inlined into the call site by the Kotlin compiler, so stubbing them has no effect.
  • Since Mockito expects each matcher to be applied in order of method arguments, even when stubbing a function with named arguments, you need to apply matchers in that order.

  • Calling default implementation of interfaces using Answers.CALLS_REAL_METHODS is not supported.