Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: callOriginal on default interface method
- Loading branch information
Showing
4 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
.../mockk-agent/src/jvmMain/kotlin/io/mockk/proxy/jvm/util/DefaultInterfaceMethodResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.mockk.proxy.jvm.util | ||
|
||
import io.mockk.proxy.jvm.advice.MethodCall | ||
import java.lang.reflect.Method | ||
import java.lang.reflect.Modifier | ||
|
||
class DefaultInterfaceMethodResolver { | ||
|
||
companion object { | ||
|
||
internal fun getDefaultImplementationOrNull(mock: Any, method: Method, arguments: Array<Any?>): MethodCall? = | ||
findDefaultImplMethod(method) | ||
?.let { | ||
val defaultImplMethodArguments = arrayOf(mock, *arguments) | ||
MethodCall(mock, it, defaultImplMethodArguments) | ||
} | ||
|
||
private fun findDefaultImplMethod(method: Method): Method? = | ||
method.takeIf { Modifier.isAbstract(it.modifiers) } | ||
?.declaringClass | ||
?.let { declaringClass -> | ||
findDefaultImplsClass(declaringClass) | ||
?.runCatching { | ||
getMethod(method.name, declaringClass, *method.parameterTypes.requireNoNulls()) | ||
} | ||
?.getOrNull() | ||
?.takeIf { Modifier.isStatic(it.modifiers) } | ||
} | ||
|
||
private fun findDefaultImplsClass(clazz: Class<*>): Class<*>? = | ||
clazz.takeIf { it.isInterface && isKotlinClass(it) } | ||
?.classes?.firstOrNull { it.simpleName == "DefaultImpls" && Modifier.isStatic(it.modifiers) } | ||
|
||
private fun isKotlinClass(clazz: Class<*>): Boolean { | ||
return clazz.isAnnotationPresent(Metadata::class.java) | ||
} | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
...es/mockk-agent/src/jvmTest/java/io/mockk/proxy/util/DefaultInterfaceMethodResolverTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.mockk.proxy.util | ||
|
||
import io.mockk.proxy.jvm.util.DefaultInterfaceMethodResolver | ||
import org.junit.jupiter.api.Assertions.assertNotNull | ||
import org.junit.jupiter.api.Assertions.assertNull | ||
import kotlin.test.Test | ||
|
||
|
||
class DefaultInterfaceMethodResolverTest { | ||
|
||
interface A { | ||
fun method() | ||
fun defaultMethod(arg: String): String { | ||
return "Arg: $arg" | ||
} | ||
} | ||
|
||
class B : A { | ||
fun subclassMethod(arg: String) {} | ||
override fun method() { | ||
} | ||
} | ||
|
||
@Test | ||
fun `should return MethodCall when default implementation exists`() { | ||
val subclass = B() | ||
val method = A::class.java.getMethod("defaultMethod", String::class.java) | ||
val arguments = arrayOfNulls<Any>(1).also { it[0] = "arg" } | ||
|
||
val result = DefaultInterfaceMethodResolver.getDefaultImplementationOrNull(subclass, method, arguments) | ||
|
||
assertNotNull(result) | ||
|
||
} | ||
|
||
@Test | ||
fun `should return null when is concrete class method`() { | ||
val subclass = B() | ||
val method = B::class.java.getMethod("subclassMethod", String::class.java) | ||
val arguments = arrayOfNulls<Any>(1).also { it[0] = "arg" } | ||
|
||
val result = DefaultInterfaceMethodResolver.getDefaultImplementationOrNull(subclass, method, arguments) | ||
|
||
assertNull(result) | ||
} | ||
|
||
@Test | ||
fun `should return null when method is overwritten`() { | ||
val subclass = B() | ||
val method = A::class.java.getDeclaredMethod("method") | ||
val arguments = arrayOfNulls<Any>(0) | ||
|
||
val result = DefaultInterfaceMethodResolver.getDefaultImplementationOrNull(subclass, method, arguments) | ||
|
||
assertNull(result) | ||
} | ||
|
||
@Test | ||
fun `should return null when method is not a Kotlin class`() { | ||
val subclass = ArrayList<Any>() | ||
val method = ArrayList::class.java.getDeclaredMethod("add", Any::class.java) | ||
val arguments = arrayOfNulls<Any>(1).also { it[0] = "element" } | ||
|
||
val result = DefaultInterfaceMethodResolver.getDefaultImplementationOrNull(subclass, method, arguments) | ||
|
||
assertNull(result) | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
modules/mockk/src/jvmTest/kotlin/io/mockk/it/CallOriginalOnDefaultInterfaceMethodTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.mockk.it | ||
|
||
import io.mockk.* | ||
import kotlin.test.Test | ||
|
||
class CallOriginalOnDefaultInterfaceMethodTest { | ||
|
||
interface A { | ||
fun method1(items: List<Int>) | ||
fun method2(items: List<Int>) | ||
fun defaultMethod(callMethod2: Boolean) { | ||
method1(listOf(1, 2, 3)) | ||
if (callMethod2) | ||
method2(listOf(4, 5, 6)) | ||
} | ||
} | ||
|
||
@Test | ||
fun `should call the original default method when spy the class`() { | ||
val spy = spyk<A>() | ||
every { spy.defaultMethod(any()) } answers { callOriginal() } | ||
|
||
spy.defaultMethod(callMethod2 = true) | ||
|
||
verify { spy.method1(listOf(1, 2, 3)) } | ||
verify { spy.method2(listOf(4, 5, 6)) } | ||
} | ||
|
||
@Test | ||
fun `should call the original default method when mock the class`() { | ||
val mock = mockk<A>() | ||
every { mock.defaultMethod(any()) } answers { callOriginal() } | ||
every { mock.method1(any()) } just runs | ||
every { mock.method2(any()) } just runs | ||
|
||
mock.defaultMethod(callMethod2 = true) | ||
|
||
verify { mock.method1(listOf(1, 2, 3)) } | ||
verify { mock.method2(listOf(4, 5, 6)) } | ||
} | ||
|
||
} |