20

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.

Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
  • 2
    Why cannot you use assertj? – yole Aug 16 '17 at 10:15
  • Please give examples of how it doesn't work in your case, and/or for cases you where you do not know how to do the equivalent in Kotlin – Les Aug 16 '17 at 10:21
  • 5
    I've been using assertk for a while (https://github.com/willowtreeapps/assertk). For my needs it was and is perfect. Even if it's missing some matchers, it's so easy to create new ones using that same lib, that it's literally all I ever need. I am not an author of the lib or associated with the author in any way, JFYI :) – NeverwinterMoon Jul 31 '18 at 05:20
  • 1
    One of problems with assertJ and Kt is custom messages because `as` is a keyword in Kotlin – Alissa Oct 03 '18 at 11:23

2 Answers2

25

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

Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
  • 1
    @MarioKutlev I added some simple samples to the answer - also, it would be easier if you just add your examples to the question(or create a new one since this one is about libraries) – Grzegorz Piwowarek Aug 16 '17 at 13:20
8

You are probably no longer searching for an assertion library but just in case you are not yet happy with your current choice, have a look at https://github.com/robstoll/atrium

It supports inter alia:

  • assertion groups
  • assertion functions for nullable types
  • property assertions
  • method assertions
  • postulate that a Throwable was thrown

As well as more advanced features such as sophisticated assertion builders, support for i18n and more.

The examples in the README will give you a nice overview: https://github.com/robstoll/atrium/blob/master/README.md#examples

Robert Stoll
  • 371
  • 3
  • 11
  • This isn’t exactly the same as AssertJ. For example, in AssertJ you only need to import `assertThat` to test `assertThat(1).isEqualTo(1)`. This is one of the selling point: import the assertion, not the matchers. In Atrium, you need to import both `expect` (assertion) and `toBe` (matcher) to say `expect(1).toBe(1)`. – Franklin Yu Jul 30 '19 at 21:42
  • @FranklinYu that's true, Atrium requires two imports `expect` and the corresponding API. However, if you use a star import for the API then that's it. AssertJ does not support multiple APIs as Atrium does, there is always a trade-off I guess – Robert Stoll Aug 28 '19 at 20:56