Home > Mobile >  Kotlin Junit5 ValueSource as an array variable
Kotlin Junit5 ValueSource as an array variable

Time:11-23

I have a map of key and values and I want to add the keys as array to ValueSource but I've got error.Where am I wrong?

Here's my code

   private val id = mapOf("abc" to 100016040,"def" to 955803,"ghi" to 955804,"jkl" to 955805)
    private val ids:Array<String> = id.keys.toTypedArray()
    
    @ParameterizedTest
    @ValueSource(strings = ids)
    fun idConverterImpl(memberId: String) {
}

CodePudding user response:

Yours keys mapping and fetching is ok, you are probably having some other kind of issue my guess is private your tests cant get to data

CodePudding user response:

The arguments to annotations must be compile time constants. ids is not a compile time constant. It is the value of id.keys.toTypedArray(), which must be computed at runtime.

You can instead write it inline like this:

@ParameterizedTest
@ValueSource(strings = ["abc", "def", "ghi", "jkl"])
fun idConverterImpl(memberId: String) { ... }

If you don't want to duplicate the keys of your map in multiple places, you can instead use a MethodSource. This allows you to non compile-time constants as the parameters of your test, by providing a method that will generate the parameters.

You will need to make your map and values static:

companion object {
    @JvmStatic
    private val id = mapOf("abc" to 100016040, "def" to 955803, "ghi" to 955804, "jkl" to 955805)

    @JvmStatic
    private val ids by lazy { id.keys }
}

By using a property delegate on ids, I made Kotlin generate a getIds method, that I can then refer to using MethodSource:

@ParameterizedTest
@MethodSource("getIds")
fun idConverterImpl(memberId: String) { ... }
  • Related