The assertj library has the java function
public static void assertSoftly(Consumer<SoftAssertions> softly) {
SoftAssertionsProvider.assertSoftly(SoftAssertions.class, softly);
}
I noticed accidentally that you can pass a lambda receiver function into this and everything just works…
fun assertSoftly(block: SoftAssertions.() -> Unit) {
SoftAssertions.assertSoftly(block) // compiles and works
}
This makes for a much nicer kotlin-friendly experience
assertSoftly {
assertThat("a").isEqualTo("b") // no need to prefix assertThat with 'it', as we would need to if we were using a Consumer
}
Technically I would expect only something like the below to work
fun assertSoftly(block: (SoftAssertions) -> Unit) {
SoftAssertions.assertSoftly(block) // also compiles and works
}
I tried looking up where the first behavior would be explained (lambda receiver could be used as consumer) but couldn’t find anything. Is this behavior explained anywhere?
Thanks