Skip to content

Default arguments in open Kotlin function cause untestable branches #882

@clydebarrow

Description

@clydebarrow

Steps to reproduce

Test this class:

open class TestClass {

    open fun testFun(arg1: String, arg2: String = "abc"): String {
        return arg2
    }
}

with this junit test code:

class TestClassTest {

    @Test
    fun testFun() {
        val testClass = TestClass()
        assertEquals("abc", testClass.testFun("123"))
        assertEquals("def", testClass.testFun("123", "def"))
        assertEquals("abc", testClass.testFun("123", "abc"))
    }
}

and examine the coverage of TestClass

Environment

JaCoCo version: 0.8.4
Operating system: Macos
Tool integration: Gradle

Expected behaviour

TestClass should show complete coverage

Actual behaviour

The synthetic function testFun$default(TestClass, String, String, int, Object) which implements default arguments is shown as missing coverage of one branch and 5 instructions. See image below.

The issue occurs only with open functions. It appears Kotlin inserts additional code which is presumably invoked only when the function is overridden. The particular use-case which led to this is in a Spring Boot environment where annotations such as @Transactional automatically make classes open, rather than the Kotlin default of final.

Screenshot 2019-05-15 17 25 02

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions