Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there Kotlin equivalent for AssertJ library? [closed]

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.

like image 271
Mario Kutlev Avatar asked Aug 16 '17 10:08

Mario Kutlev


People also ask

What is assert in Kotlin?

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.

What is AssertJ used for?

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.


2 Answers

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

like image 55
Grzegorz Piwowarek Avatar answered Oct 07 '22 15:10

Grzegorz Piwowarek


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

like image 8
Robert Stoll Avatar answered Oct 07 '22 15:10

Robert Stoll