Skip to content

www.rolfje.com

Testing Kotlin extension functions

Posted on 2020-07-012025-11-13 By rolfje No Comments on Testing Kotlin extension functions

This might seem obvious for many Kotlin programmers but I did not realize that extension functions are also applied to mocks. This means that if you have a class you can not instantiate for some reason, like the constructor is private and in another package, or the class is abstract, you can still test your extension functions by simply mocking it.

Let’s say I have an extension function on the com.sun.java.swing.action.ActionManager which returns “true” when the action manager contains a “save” action. (Yes it is a silly example I know).

/** Returns true if the actionManager has a "save" action */
fun ActionManager.canSave(): Boolean =
    getAction("save") == null

Of course we want to test our extension function, but writing a complete subclass of ActionManager just to test the extension function would be silly. Instead you can simply mock it with mockk:

import io.mockk.coEvery
import io.mockk.mockk
...
@Test
internal fun testCanSaveExtensionFunction() {
    val mockActionManager: ActionManager = mockk()
    coEvery { mockActionManager.getAction("save") } answers { mockk() }
    assertEquals(true, mockActionManager.canSave())
}

This test will not only run your extension function, but you can even verify that the extension function calls the correct functions on the extended class because mockk lets you check invocations.

It seems like a silly little tip, but I liked it so much when I found this out I thought I’d share it with you. Hope to be more active on the blog again, until then happy coding!

Software Tags:Kotlin

Post navigation

Previous Post: Voor uw eigen veligheid
Next Post: Methode Buijs uitgelegd

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

         
@rolfje.com
www.youtube.com/watch?v=_ZBK...
3 hours ago
@rolfje.com
www.youtube.com/watch?v=anwK...
2 days ago
Reposted by @rolfje.com
@michaelspicer.bsky.social
What if James Bond had to do a few days in the office. Short film below.

youtu.be/hhd0hOIDljA
2 days ago

Recent Comments

  • rolfje on Spring SystemPropertyInitializingBean
  • plinko idle clicker on Spring SystemPropertyInitializingBean
  • rolfje on The podcast problem (fixed)
  • SirenMix on The podcast problem (fixed)
  • egorio on Volkswagen e-Up! 80% charge trick

Tags

Anonimatron Apple backup design DIY DRM eclipse environment fix Fun gmail google hacking hamradio Hardware helicopter iphone ipod iTunes Java Kawasaki Keynote linux modelling motorcycle music news opinion oracle osx photo photography programming repair review security Software technology Time Machine Ubuntu usability Utilities vacation windows Workshop

Categories

  • Apple (107)
  • Divorce (1)
  • Electronics (5)
  • Fun (58)
  • Games (8)
  • Hardware (74)
  • Microsoft (18)
  • Racing (16)
  • Software (144)
  • Uncategorized (69)
  • Workshop (23)

Archives

Brought to you without adds, banners, cookies or tracking. This one is on me. Yes, life can be this good. Pay it forward.
AI policy - Privacy policy

Copyright © 2026 www.rolfje.com.

Powered by PressBook WordPress theme