In Kotlin the powerful construct of delegation can be used to extend functionality of existing interfaces by reusing existing implementations.

class Demo : Map by HashMap {}


  • What should I be testing? Testing hashmap from the example is not the target of this test. It seems very verbose to verify the complete implementation, I would rather like to verify that the delegation of the proper fields take place.
  • When using mutation testing, e.g. using PItest, how do I catch all mutations? The report shows quite a few mutations, correctly I believe. The Kotlin compiler creates byte code for all delegations.

1 Answer 1


Firstly, you shouldn't unit-test the implementation of HashMap (of course) as it isn't your responsibility in class Demo. (+ you should "presume" that HashMap impl is correct).

Regarding verifying the delegation, it sounds to me that it should come up as a warning by static analyzer/compiler if you delegated and you decided to hide/re-implement functions. In that case, you should provide a reason for dismissing such warning + document it (and in case needed writing a simple test as part of your CI that enforces this hiding). (+ Again, regarding the mechanism of delegation you should "presume" that its impl is correct).

Regarding mutation testing, (I'm not aware of what features PItest provide) false-positives are inherent part of mutation testing and it requires effort to look and research the results (However, it can be really worth it). If you can disable mutations on functions of the delegated object (or the delegated object itself) it's a trade-off you need to decide.

I suggest that you invest time on unit-testing of parts in the class that you added + to write "integration" tests for functions in your class that use the delegated object functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.