Super-simple and super-fast Mocking library. Use when you really don't want to import an entire mocking library to slow down your build, or just want some simple replacement of an interface.
- ✅ Simple
- ✅ Fast
- ✅ Relaxed/Strict modes
- ✅ Coroutines
- ✅ Functions
- ❌ Verification
- ❌ Argument capture
- ❌ Partial mocking
- ❌ Spies
- ❌ Any evil Powermock nonsense
- ❌ Spring-support
- ❌ Multiplatform
- ❌ Annotations
- ❌ Blockchain
- ❌ AI-powered
In Gradle, install the ForkHandles BOM and then this module in the dependency block:
implementation(platform("dev.forkhandles:forkhandles-bom:X.Y.Z"))
implementation("dev.forkhandles:mock4k")
Import from dev.forkhandles.mock4k
, then apply the unique delegate'n'stub technique. (patent pending).
interface Wallet {
fun pay(item: String, coins: Int): Int?
}
class AppleStore(private val wallet: Wallet) {
fun buyMacBook() = wallet.pay("MacBook", 9999)
suspend fun seeGenius() = "hello!"
fun buyMacbookUsing(pay: (String, Int) -> Int?) = pay("MacBook", 9999)
}
class AppleStoreTest {
@Test
fun `buy macbook in strict mode`() {
// when there are calls expected you can delegate'n'stub
val appleStore = AppleStore(object : Wallet by mock() {
override fun pay(item: String, coins: Int): Int? {
assertThat(item, equalTo("MacBook"))
assertThat(coins, equalTo(9999))
return -9999
}
})
assertThat(appleStore.buyMacBook(), equalTo(-9999))
}
@Test
fun `buy macbook in relaxed mode`() {
// relaxed mode returns null by default
val appleStore = AppleStore(mock(MockMode.Relaxed))
assertThat(appleStore.buyMacBook(), equalTo(null))
}
@Test
fun `see genius`() {
val appleStore = AppleStore(mock())
runBlocking {
// suspend calls
assertThat(appleStore.seeGenius(), equalTo("hello!"))
}
}
@Test
fun `bus using someone else's money`() {
val appleStore = AppleStore(mock())
appleStore.buyMacbookUsing(mock())
}
}
- All contributors
- Inspired by left-pad