Skip to content

A minimalist faking framework exclusively for verifying interactions

License

Notifications You must be signed in to change notification settings

anthonycr/Mockingbird

Repository files navigation

Mockingbird

A minimalist faking framework exclusively for verifying interactions. It generates fake implementations of interfaces that can be verified during a test using Kotlin Symbol Processing (KSP). It does not allow mocking behavior or return values, and does not support mocking concrete classes.

Why?

Other testing frameworks exist, like MockK, that support full mocking of all types, a wide range of features, and have significantly more utility than this minimalist framework. However, mocking objects is frequently viewed as an anti-pattern, as mocking an object can cover up poor class design by making untestable code testable. Tests that heavily utilize mocks can lose their utility and even obscure bugs, or can become difficult to understand and refactor. Additionally, mocks are usually slow to construct, which in turn slows down test execution.

Fakes are less forgiving then mocks and require better class design in order to support testing. Because they are less flexible, they have less potential to be abused. However, it can be annoying to verify interactions on fakes because they are usually handwritten. Mockingbird auto-generates fake implementations of interfaces that have Unit function types - types usually performing side effects that are most often verified by a mocking framework.

Usage

Maven Central Version

Mockingbird is available on Maven Central. Include the following dependencies in your build.gradle.kts file.

testImplementation("com.anthonycr.mockingbird:core:<latest_version>")
kspTest("com.anthonycr.mockingbird:processor:<latest_version>")

Make sure you have the KSP Gradle plugin enabled as well.

Within your test, add the following annotation to a property that you wish to verify. Note that the property you are verifying must be an interface with all functions returning Unit.

import com.anthonycr.mockingbird.core.Verify
import com.anthonycr.mockingbird.core.fake
import com.anthonycr.mockingbird.core.verify

interface Analytics {
    fun trackEvent(event: String)
}

class ClassToTest(private val analytics: Analytics) {
    fun doSomething() {
        analytics.trackEvent("doSomething was called!")
    }
}

class ClassToTestTest {

    @Verify
    val analytics: Analytics = fake()

    @Test
    fun `analytics event for doSomething was triggered`() {
        val classToTest = ClassToTest(analytics)

        classToTest.doSomething()

        verify(analytics) {
            analytics.trackEvent("doSomething was called!")
        }
    }
}

Note: The com.anthonycr.mockingbird.core.fake function is generated at compile time, so it will not be available to import until KSP runs.

About

A minimalist faking framework exclusively for verifying interactions

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages