title: Testing extension functions inside classes
body: <p>If we want to test an extension function on a type, we can create an instance of this type, call the function and check the returned value. But what about testing extension functions defined inside classes?</p>
  
  <pre><code>abstract class AbstractClass&lt;T&gt; {
      fun doStuff(): T = "Hello".foo()
  
      abstract fun String.foo(): T
  }
  
  class SubClass1: AbstractClass&lt;Int&gt;() {
      override fun String.foo(): Int = 1
  }
  
  class SubClass2: AbstractClass&lt;Boolean&gt;() {
      override fun String.foo(): Boolean = true
  }
  </code></pre>
  
  <p>How do we test the logic of the methods <code>foo()</code> in classes <code>SubClass1</code> and <code>SubClass2</code>? Is it even possible?</p>
  
  <p>I know I can change the design to test it. Two possibilities have occurred to me:</p>
  
  <ol>
  <li><p>Don't use extension functions. ¯\_(ツ)_/¯</p>
  
  <pre><code>abstract class AbstractClass&lt;T&gt; {
      fun doStuff(): T = foo("Hello")
  
      abstract fun foo(string: String): T
  }
  
  class SubClass1: AbstractClass&lt;Int&gt;() {
      override fun foo(string: String): Int = 1
  }
  </code></pre>
  
  <p>Then we can create an object <code>SubClass1</code>, call <code>foo()</code> and check the returned value.</p></li>
  <li><p>Create additional extension functions with <code>internal</code> visibility just to test the logic.</p>
  
  <pre><code>class SubClass1: AbstractClass&lt;Int&gt;() {
      override fun String.foo(): Int = internalFoo()
  }
  
  internal fun String.internalFoo(): Int = 1
  </code></pre>
  
  <p>Then we can create an object <code>String</code>, call <code>internalFoo()</code> and check the returned value. However, I don't like this solution because we could change the body of <code>override fun String.foo(): Int</code> and our test would pass.</p></li>
  </ol>
  
  <p>So, <strong>is it possible to test extension functions inside classes</strong>? If not, how would you change your design in order to test their logic?</p>