Get your default answers back!

Maven Central

Installation

dependencies {
  testImplementation("com.episode6.reflectivemockk:reflective-mockk:1.0.0")
}

reflective-mockk v1.0.0 is compiled against Kotlin v1.7.10

Usage

Create a new mockk with a default answer…

val mock = reflectiveMockk<SomeBuilder> {
  defaultAnswer { self }
}


Apply a reflective stubs to an existing mockk…

val mock = mockk<SomeBuilder>()
mock.reflectiveStubs {
  defaultAnswer { self }
}


Only stub methods based on return type…

val mock = reflectiveMockk<SomeBuilder> {
  answerEveryCallIn(normalMemberFunctions.filterReturnType<SomeBuilder>()) { self }
}


Stub methods manually…

val mock = reflectiveMockk<SomeBuilder> {
  memberFunctions
    .filter { it.returnType.classifier == SomeBuilder::class }
    .filter { !it.isSuspend }
    .forEach { call ->
      everyCallTo(call) returns self
      // OR every { callTo(call) } returns self
    }
}


Stub suspend methods…

val mock = reflectiveMockk<SomeBuilder> {
  coAnswerEveryCallIn(suspendMemberFunctions) { awaitCancellation() }
}


Stub suspend methods manually…

val mock = reflectiveMockk<SomeBuilder> {
  memberFunctions
    .filter { it.isSuspend }
    .forEach { call ->
      coEverySuspendCallTo(call) coAnswers { awaitCancellation() }
      // OR coEvery { suspendCallTo(call) } coAnswers { awaitCancellation() }
    }
}

Readme Index