Skip to main content

Life Before MediatorK

Testing without a mocking library

The biggest testing win from MediatorK is what it does to your ViewModel constructor. A typical ViewModel that manages its own dependencies directly ends up with 10+ constructor parameters; see The Promise for the full before/after comparison.

To instantiate such a ViewModel in a test you must stub every one of those parameters, even if the test only touches two of them. Every new use-case added to the ViewModel breaks every existing test that constructs it.

With MediatorK the constructor collapses to one dependency:

class InitialViewModel(
private val mediator: Mediator,
) : ViewModel()

Every test now starts the same way:

val vm = InitialViewModel(testMediator { }) // real empty mediator — no handlers registered
val vm = InitialViewModel(testMediator {}) // register handler bodies as needed

The use-cases, metrics reporters, toggle observers, and performance trackers all move into individual handle { } lambdas. Each handler is tested in isolation. The ViewModel test only verifies how the ViewModel reacts to success or failure; it never needs to know which use-cases exist.


Installation

Add the test utilities artifact (see Installation for all coordinates):

dependencies {
testImplementation("io.github.fajrbahr:mediatork-test:0.6.3")
}

Next

testMediator