Home > other >  SonarCloud coverage stating that class property with JsonProperty annotation not covered by tests
SonarCloud coverage stating that class property with JsonProperty annotation not covered by tests

Time:10-24

I have a Kotlin project with Spring, and I created a class that looks like the following:

@JsonIgnoreProperties(ignoreUnknown = true)
data class Response(
    val id: String,
    @JsonProperty("quantity_of_days")
    val quantityOfDays: Int,
)

And my SonarCloud reports state that the quantityOfDays line is not covered by tests:

SonarCloud coverage message

This line is accessed multiple times inside my tests, and I even created one specifically to instantiate an object of that class. However, this line is still marked as not covered.

I wonder if it has something to do with the annotation, and if so, how do I ignore or force this line to be covered?

CodePudding user response:

Ok so, it was necessary to write some very specific tests to cover that:


class ResponseTest {

    @Test
    fun `create response from json with underline attribute`() {
        val id = "123"
        val quantityOfDays = 1
        val response = Response(id, quantityOfDays)

        val value = Mapper.objectMapper.readValue(
            """
                {
                    "id": "$id",
                    "quantity_of_days": $quantityOfDays
                }
            """.trimIndent(), Response::class.java)

        assertThat(value).isEqualTo(response)
    }

    @Test
    fun `create response from json with camel case attribute`() {
        val response = ResponseBuilder().build()
        val json = Mapper.objectMapper.writeValueAsString(response)

        val value = Mapper.objectMapper.readValue(json, Response::class.java)

        assertThat(value).isEqualTo(response)
    }
}

I am not sure if that is the best solution, maybe there is a way to make the coverage ignore that in specific, but I could not find it. But it works.

  • Related