Unit testing is instrumental in enhancing code quality, documenting system behavior, and preventing regressions in contemporary software development. One of the current limitations of the most popular existing unit-testing frameworks for the Kotlin language (e.g., MockK and Mockito) is that they are inherently tied to the Java Virtual Machine (JVM), restricting their use to JVM-compatible environments. To overcome this limitation, in this paper we present an extended version of Mockative, a Kotlin-specific mocking framework designed to operate across multiple platforms beyond the JVM. While originally Mockative was limited to interface mocking, we have enhanced it to support class mocking and spying through the utilization of Kotlin Symbol Processing (KSP) and KotlinPoet libraries. We show that Mockative is a viable alternative to traditional JVM-bound frameworks, particularly in Kotlin/Native environments. Our extension can mock simple classes up to four times faster than MockK and Mockito, although it exhibits a marginal performance decline in scenarios involving classes with extensive method definitions.

错误:搜索内容不能为空,请输入英文关键词
错误:关键词超出字数限制,请精简
高级检索

Mocking Classes in Kotlin with Mockative

  • Kristian Degn Abrahamsen,
  • Jacopo Mauro

摘要

Unit testing is instrumental in enhancing code quality, documenting system behavior, and preventing regressions in contemporary software development. One of the current limitations of the most popular existing unit-testing frameworks for the Kotlin language (e.g., MockK and Mockito) is that they are inherently tied to the Java Virtual Machine (JVM), restricting their use to JVM-compatible environments. To overcome this limitation, in this paper we present an extended version of Mockative, a Kotlin-specific mocking framework designed to operate across multiple platforms beyond the JVM. While originally Mockative was limited to interface mocking, we have enhanced it to support class mocking and spying through the utilization of Kotlin Symbol Processing (KSP) and KotlinPoet libraries. We show that Mockative is a viable alternative to traditional JVM-bound frameworks, particularly in Kotlin/Native environments. Our extension can mock simple classes up to four times faster than MockK and Mockito, although it exhibits a marginal performance decline in scenarios involving classes with extensive method definitions.