org.mockito.kotlin.BDDMockito.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-kotlin Show documentation
Show all versions of mockito-kotlin Show documentation
Using Mockito with Kotlin.
The newest version!
/*
* The MIT License
*
* Copyright (c) 2018 Niek Haarman
* Copyright (c) 2007 Mockito contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.mockito.kotlin
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
import org.mockito.BDDMockito
import org.mockito.BDDMockito.BDDMyOngoingStubbing
import org.mockito.BDDMockito.Then
import org.mockito.invocation.InvocationOnMock
import org.mockito.kotlin.internal.SuspendableAnswer
import org.mockito.stubbing.Answer
import kotlin.reflect.KClass
/**
* Alias for [BDDMockito.given].
*/
fun given(methodCall: T): BDDMockito.BDDMyOngoingStubbing {
return BDDMockito.given(methodCall)
}
/**
* Alias for [BDDMockito.given] with a lambda.
*/
fun given(methodCall: () -> T): BDDMyOngoingStubbing {
return given(methodCall())
}
/**
* Alias for [BDDMockito.given] with a suspending lambda
*
* Warning: Only last method call can be stubbed in the function.
* other method calls are ignored!
*/
fun givenBlocking(methodCall: suspend CoroutineScope.() -> T): BDDMockito.BDDMyOngoingStubbing {
return runBlocking { BDDMockito.given(methodCall()) }
}
/**
* Alias for [BDDMockito.then].
*/
fun then(mock: T): BDDMockito.Then {
return BDDMockito.then(mock)
}
/**
* Alias for [Then.should], with suspending lambda.
*/
fun Then.shouldBlocking(f: suspend T.() -> R): R {
val m = should()
return runBlocking { m.f() }
}
/**
* Alias for [BDDMyOngoingStubbing.will]
* */
infix fun BDDMyOngoingStubbing.will(value: Answer): BDDMockito.BDDMyOngoingStubbing {
return will(value)
}
/**
* Alias for [BBDMyOngoingStubbing.willAnswer], accepting a lambda.
*/
infix fun BDDMyOngoingStubbing.willAnswer(value: (InvocationOnMock) -> T?): BDDMockito.BDDMyOngoingStubbing {
return willAnswer { value(it) }
}
/**
* Alias for [BBDMyOngoingStubbing.willAnswer], accepting a suspend lambda.
*/
infix fun BDDMyOngoingStubbing.willSuspendableAnswer(value: suspend (InvocationOnMock) -> T?): BDDMockito.BDDMyOngoingStubbing {
return willAnswer(SuspendableAnswer(value))
}
/**
* Alias for [BBDMyOngoingStubbing.willReturn].
*/
infix fun BDDMyOngoingStubbing.willReturn(value: () -> T): BDDMockito.BDDMyOngoingStubbing {
return willReturn(value())
}
/**
* Alias for [BBDMyOngoingStubbing.willThrow].
*/
infix fun BDDMyOngoingStubbing.willThrow(value: () -> Throwable): BDDMockito.BDDMyOngoingStubbing {
return willThrow(value())
}
/**
* Sets a Throwable type to be thrown when the method is called.
*
* Alias for [BDDMyOngoingStubbing.willThrow]
*/
infix fun BDDMyOngoingStubbing.willThrow(t: KClass): BDDMyOngoingStubbing {
return willThrow(t.java)
}
/**
* Sets Throwable classes to be thrown when the method is called.
*
* Alias for [BDDMyOngoingStubbing.willThrow]
*/
fun BDDMyOngoingStubbing.willThrow(
t: KClass,
vararg ts: KClass
): BDDMyOngoingStubbing {
return willThrow(t.java, *ts.map { it.java }.toTypedArray())
}
/**
* Sets consecutive return values to be returned when the method is called.
* Same as [BDDMyOngoingStubbing.willReturn], but accepts list instead of varargs.
*/
inline infix fun BDDMyOngoingStubbing.willReturnConsecutively(ts: List): BDDMyOngoingStubbing {
return willReturn(
ts[0],
*ts.drop(1).toTypedArray()
)
}