Search code examples
kotlinassertionskotlintest

Is there Kotlin equivalent for AssertJ library?


I am converting some tests from Java to Kotlin. For Java tests I use AssertJ library which is very powerful and has rich set of assertions. My problem is that for Kotlin tests I can not use AssertJ and Kotlin JUnit (org.jetbrains.kotlin:kotlin-test-junit) has very limited set of assertions.

Is there Kotlin equivalent for AssertJ or better way for asserts?

I found Kluent library but I'm still not sure if this is the best library to use.


Solution

  • There is no official equivalent but basic AssertJ is still usable in many cases and looks quite fine:

    assertThat(info)
      .containsKey("foo")
    
    
    assertThatThrownBy { session.restTemplate.postForLocation("foo", {}) }
      .isExactlyInstanceOf(HttpClientErrorException::class.java)
    

    If you want dedicated wrappers, this early-stage project is trying to achieve this: https://github.com/wuan/assertj-core-kotlin