1

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 {}

Questions:

  • 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.
Michiel Leegwater
  • 1,172
  • 4
  • 11
  • 27
  • 2
    Why would you want to test the delegation? It is not your responsibility to test the Kotlin compiler – gpunto Aug 02 '19 at 17:04
  • I agree that it is not my responsibility to test the kotlin compiler. But it is to test my class. I would like to know how to do that without testing the kotlin compiler or the HashMap. – Michiel Leegwater Aug 02 '19 at 17:08
  • 1
    Is your `Demo` class actually a `Map`? Then I guess you'd have to test all `HashMap` functionality in the context of your class. If you figure out a way to use composition instead of inheritance in this use case you'd hopefully have a lot less code to test. – Egor Aug 02 '19 at 17:56
  • The test ability improves when I use `class Demo(map: Map = HashMap()): Map by map {}`. Then I can mock the map. – Michiel Leegwater Aug 02 '19 at 20:31
  • Testing whether the delegate is transparent is detailed in https://stackoverflow.com/questions/22225663/checking-in-a-unit-test-whether-all-methods-are-delegated – Michiel Leegwater Aug 02 '19 at 21:00

0 Answers0