assertj: Ambiguous signature for extracting() when using kotlin

Summary

When trying to use .extracting() with lambdas in kotlin, the call is ambigous and will not compile out-of-the-box. I think this is due to

extracting(ThrowingExtractor<? super ELEMENT, V, EXCEPTION> extractor) and extracting(Function<? super ELEMENT, V> extractor)

have the same effective signature because in kotlin, there are no checked exceptions (thank god).

Example

assertThat(highlights).hasSize(6).extracting(HighlightInfo::getText).doesNotContain("assertThat")
// nor does this work
assertThat(highlights).hasSize(6).extracting { it.text }.doesNotContain("assertThat")

I’ve got no idea on how to fix that in a backwards compatible manner other than to have a different method name for kotlin that won’t clash or trying to add an extension that will cope with it.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 1
  • Comments: 16 (9 by maintainers)

Most upvoted comments

I’ve found that the below works for me:

assertThat(highlights).hasSize(6).extracting<String> { it.text }.doesNotContain("assertThat")

As Joel said, we’re not that happy about making any breaking changes in version 3, but version 4 might be the right time to rethink how to better support Kotlin.

Would it be really necessary to have a separate library on top of assertj-core or could we do something like JUnit did in Assertions.kt?

I mostly use AssertJ with Kotlin these days. Even my teammates that still code mostly in Java are writing their tests in Kotlin. AssertJ generally works great from Kotlin, but little things like this would make it even better. I’m psyched to see this re-opended, and hear of the potential for even better Kotlin compatibility in version 4.