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.
Use it to test object state. assert(Boolean) throws AssertionError when its argument is false (but only if JVM assertions are enabled with -ea ). Use it to clarify outcomes and check your work.
The AssertJ project provides fluent assertion statements for test code written in Java. These assert statements are typically used with Java JUnit tests. The base method for AssertJ assertions is the assertThat method followed by the assertion.
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
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With