Home > Software engineering >  How to suppress Kotlin unused parameter warning in all test classes?
How to suppress Kotlin unused parameter warning in all test classes?

Time:03-22

In parameterized tests I use hint parameter to clarify test case naming. From the static analyzer point of view this parameter is never used, so this warning from kotlin-maven-plugin appears in the build log:

[WARNING] /Users/test/TestSizeCreation.kt: (42, 10) Parameter 'hint' is never used

How to suppress such warnings globally in all tests?


Example of test with hint:

@ParameterizedTest(name = "Size {index}: {0}")
@MethodSource("invalidAges")
fun shouldFailToCreateAge(hint: String, sizeCandidate: Int) {
    assertThatThrownBy { Size(sizeCandidate) }
        .isInstanceOf(InvalidInput::class.java)
        .hasMessageStartingWith("Could not recognize a se: ")
}

companion object {

    @JvmStatic
    fun invalidAges(): Stream<Arguments> =
        Stream.of(
            arguments("negative", -5),
            arguments("zero", 0),
            arguments("too much", 1000)
        )
}

CodePudding user response:

Two possible options (there may be more):

The first is to annotate the parameter as being unused, like this:

@Suppress("UNUSED_PARAMETER") either at the function or parameter level.

The second option is to use a lambda inside your test to execute the actual code, and then use an underscore to ignore the first parameter, like this:

import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.Arguments.arguments
import org.junit.jupiter.params.provider.MethodSource
import java.util.stream.Stream

class Stack {

    @ParameterizedTest(name = "Size {index}: {0}")
    @MethodSource("invalidAges")

    fun shouldFailToCreateAge(hint: String, sizeCandidate: Int) {

        process(hint, sizeCandidate) { _, size ->
             println("add your test using size here $size")
        }
    }
    
    private fun process(hint: String, sizeCandidate: Int, block: (String, Int) -> Unit) {
        block(hint, sizeCandidate)
    }

    companion object {

        @JvmStatic
        fun invalidAges(): Stream<Arguments> =
            Stream.of(
                arguments("negative", -5),
                arguments("zero", 0),
                arguments("too much", 1000)
            )
    }
}
  • Related