Search code examples
kotlinassertj

Kotlin spread operator for an array of functions does not satisfy Java Function<>... function argument definition


I need an overlay function around AssertJ extracting assertion. Its current implementation:

import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.groups.Tuple

fun <T, R> checkObjectsHaveExactPropertyValue(
    objectsList: List<T>,
    properties: Array<Function1<T, R>>,
    expectedPropertyValue: R
) {
    assertThat(objectsList)
        .extracting(*properties)
        .containsOnly(Tuple.tuple(expectedPropertyValue))
}

I want to be able to pass multiple property extractors as properties argument of my function. Then they are supplied to .extracting method.

And this is there code check and compilation error is:

Kotlin: None of the following functions can be called with the arguments supplied:
public open fun \<V : Any!\> extracting(p0: Function\<in T!, TypeVariable(V)!\>!): AbstractListAssert\<*, (MutableList\<out TypeVariable(V)!\>..List\<TypeVariable(V)!\>?), TypeVariable(V)!, ObjectAssert\<TypeVariable(V)!\>!\>! defined in org.assertj.core.api.ListAssert
public final fun extracting(vararg p0: Function\<in T!, \>!): AbstractListAssert\<*, (MutableList\<out Tuple!\>..List\<Tuple!\>?), Tuple!, ObjectAssert\<Tuple!\>!\>! defined in org.assertj.core.api.ListAssert
public open fun extracting(vararg p0: String!): AbstractListAssert\<, (MutableList\<out Tuple!\>..List\<Tuple!\>?), Tuple!, ObjectAssert\<Tuple!\>!\>! defined in org.assertj.core.api.ListAssert
public open fun extracting(p0: String!): AbstractListAssert\<*, (MutableList\<out Any!\>..List\<Any!\>?), Any!, ObjectAssert\<Any!\>!\>! defined in org.assertj.core.api.ListAssert
public open fun \<V : Any!, EXCEPTION : Exception!\> extracting(p0: ThrowingExtractor\<in T!, TypeVariable(V)!, TypeVariable(EXCEPTION)!\>!): AbstractListAssert\<*, (MutableList\<out TypeVariable(V)!\>..List\<TypeVariable(V)!\>?), TypeVariable(V)!, ObjectAssert\<TypeVariable(V)!\>!\>! defined in org.assertj.core.api.ListAssert

I don't see where I messed up.

I also tried following to debug typing:

  • .extracting(properties[0], properties[1]) - compiles, function typing is correct, but not what I need
  • different Iterable types (List, ArrayList, ...) - same error
  • vararg properties: Function1<T, R> - same error
  • properties: Array<(T) -> R> - same error

I expect there is correct typing for the argument of my function, so I can use overloaded method extracting(vararg Function<in T!, *>!) defined in org.assertj.core.api.ListAssert.


Solution

  • Although it's possible to use kotlin.jvm.functions.Function1 and java.util.function.Function interchangeably, Array<Function1> doesn't seem to work properly with extracting(Function...) when combined with the spread operator (*), as you already experienced.

    I was able to compile it only after transforming all the properties:

    fun <T, R> checkObjectsHaveExactPropertyValue(
      objectsList: List<T>,
      properties: Array<Function1<T, R>>,
      expectedPropertyValue: R
    ) {
      assertThat(objectsList)
        .extracting(*properties.map { Function(it) }.toTypedArray())
        .containsOnly(Tuple.tuple(expectedPropertyValue))
    }
    

    One remark about your assertion: if you want to extract one or more properties and compare all the resulting values against a single expectedPropertyValue, flatExtracting might be a better candidate:

    fun <T, R> checkObjectsHaveExactPropertyValue(
      objectsList: List<T>,
      properties: Array<Function1<T, R>>,
      expectedPropertyValue: R
    ) {
      assertThat(objectsList)
        .flatExtracting(*properties.map { Function(it) }.toTypedArray())
        .containsOnly(expectedPropertyValue)
    }