Given a rather large 3rd party interface, I need t...
# codereview
a
Given a rather large 3rd party interface, I need to implement only a small handful methods. (for testing, i control the calling code)
Copy code
interface Alphabet {
    fun a(): String
    fun b(): String
    // and many more
    fun z(): String
}
I thought I had a really bright idea, since the following is valid syntax:
Copy code
class A : Alphabet by TODO("NOPE") {
    override fun a() = "a"
}
I was hoping the delegation would act as with actual objects, routing calls to the override if they were present, and throwing NotImplementedError otherwise. Unfortunately, the
TODO()
is evaluated on instantiation of A() and not during method call 😞
Copy code
// Hoping for
A().a() == "a" // true
A().b() // NotImplementedError

// but got 
A() // NotImplementedError
Does anyone have an elegant way to partially implement an interface and default to NotImplementedError for the rest, without explicitly doing so for every single method?
e
and on all platforms you can build similar statically with KSP
but it would be easier to use an existing mocking library
a
Yeah, seems like alternatives are Proxy, explicitly
TODO
’ing all methods, and making a “wrapping” interface. I ended up doing the latter.
e.g.
Copy code
interface TinyAlphabet { fun a(): String }
and expecting that in the calling code instead.
d
If it’s for testing, perhaps mocking is what you want. Mockk is what I usually use.
➕ 2
Here is the MockK site