My general advice about Mockito is to try and avoid it when you can. Ryan Harter has an awesome companion post called Replacing Mocks which shows some potential pitfalls of using Mockito Mocks, as well as how to avoid using it by restructuring code. However, sometimes Mockito can be the right tool to use for mocking dependencies in a unit test when a code refactor isn’t possible due to constraints. I use Mockito-Kotlin which helps leverage Mockito in Kotlin code.
In this post I show how you can wrap a Mockito mock to avoid using verbose syntax (“whenever”, “verify”, etc.) all over the place. I call this wrapper around the Mockito mock a “Fake”.
// Fake Wrapper around Mockito Mock (See implementation below)
val fakeOven = FakeOven()
// Access and Use Mockito Mock
val oven : Oven = fakeOven.mock
// Clean API to Setup Mocks (using Wrapper)
fakeOven.givenOvenResult(OvenResult.Success)
// Clean API to Verify Mocks (using Wrapper)
fakeOven.thenOvenSetTo(temperatureFahrenheit = 350, timeMinutes = 30)
Benefits:
- Single Responsibility – Mocking logic out of your test class.
- Cleaner Tests – Avoid using “when”, “whenever” and “verify” all over the place.
- Less Duplication – Can be reused across tests and for future tests you may write.
Example: Baking with an Oven
In this example we bake a Cake which requires an Oven. We need to mock the Oven.
NOTE: We could try various approaches for this example, but for the purpose of explaining this strategy, we’ll use the “Fake” Mockito wrapper.
/** Class that uses [Oven] */
class Dessert(val oven: Oven) {
fun bakeCake(): OvenResult {
oven.setTemperatureFahrenheit(350)
oven.setTimeMinutes(30)
return oven.start()
}
}
/** Class we will use Mockito to Mock */
class Oven {
fun setTemperatureFahrenheit(tempF: Int) {
// ...
}
fun setTimeMinutes(minutes: Int) {
// ...
}
fun start(): OvenResult {
// ...
}
}
/** Whether the Oven command was successful, or something happened */
sealed class OvenResult {
object Success : OvenResult()
data class Failure(val e: Exception) : OvenResult()
}
Original Test 🤷🏽♂️
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import org.junit.Test
/** Test Dessert Baking */
class DessertTest {
@Test
fun bakeCakeSuccess() {
val oven: Oven = mock()
val dessert = Dessert(oven)
// Setup
whenever(oven.start()).thenReturn(OvenResult.Success)
// Execute Code
dessert.bakeCake()
// Verification
verify(oven).setTemperatureFahrenheit(350)
verify(oven).setTimeMinutes(30)
}
}
FakeOven – Mockito Mock Wrapper 🤔
/** Wraps the Mockito mock for reuse */
class FakeOven {
val mock: Oven = mock()
fun givenOvenResult(ovenResult: OvenResult) {
// Setup
whenever(mock.start()).thenReturn(ovenResult)
}
fun thenOvenSetTo(temperatureFahrenheit: Int, timeMinutes: Int) {
// Verification
verify(mock).setTemperatureFahrenheit(temperatureFahrenheit)
verify(mock).setTimeMinutes(timeMinutes)
}
}
Updated Test – Using Fake Mockito Wrapper ✅
class DessertTestWithFake {
@Test
fun bakeCakeSuccess() {
val fakeOven = FakeOven()
val dessert = Dessert(fakeOven.mock)
fakeOven.givenOvenResult(OvenResult.Success)
dessert.bakeCake()
fakeOven.thenOvenSetTo(
temperatureFahrenheit = 350,
timeMinutes = 30
)
}
}
We use “fakeOven.mock” to fulfill the “Oven” dependency, and control the behavior using the wrapper we have created.
val fakeOven = FakeOven()
val oven : Oven = fakeOven.mock
Conclusion
Isolate usage of Mockito, and avoid scattering it all over your tests. By using this pattern of wrapping a Mockito Mock in a fake, your tests that require Mockito are a little bit better.