How to suppress Kotlin unused parameter warning in all test classes?

1.8k views Asked by At

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("invalidSizes")
fun shouldFailToCreateSize(hint: String, sizeCandidate: Int) {
    assertThatThrownBy { Size(sizeCandidate) }
        .isInstanceOf(InvalidInput::class.java)
        .hasMessageStartingWith("Could not recognize size: ")
}

companion object {

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

There are 2 answers

0
diziaq On BEST ANSWER

I ended up using this function introduced only in the src/test context:

// this function used only to avoid "Parameter is never used" warning
// on intentionally unused parameters
fun Any?.touch() = Unit

This how it looks in a test method:

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

Why:

  1. The @Suppress("UNUSED_PARAMETER") is intended strictly for special situations in rare cases. And would be inappropriate to put it in all Parameterized tests making it noisy. It also could cause missing real cases of unused parameters, helping garbage code appear.

  2. The touch method clearly shows intention. And it looks like a minimal evil.

0
PaulNUK On

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)
            )
    }
}