Skip to content
Johan Haleby edited this page Jan 25, 2019 · 11 revisions

Awaitility has a support module called awaitility-kotlin that contains a file called AwaitilityKt (in package org.awaitility.kotlin which contains some useful extension functions.

untilCallTo

Import this extension function (import org.awaitility.kotlin.untilCallTo) and then you can write tests like this:

val myRepository : MyRepository = ...

// Wait until call to myRepository.count() is equal to 3
await().untilCallTo { myRepository.count() } matches { count -> count == 3 }

As of 3.1.5 you can also use the has function in combination with matches if you want to more easily test for properties in data class. For example if you have a data class defined like this:

data class Data(var state: String)

Then you can test e.g. that state is updated to "hello":

val data = await untilCallTo { myDataRepository.findById("dataId") } has {
     state == "hello"
}

This is the same as doing this:

await untilCallTo { myDataRepository.findById("dataId") } matches { data ->
     data?.state == "hello"
}

untilNull/untilNotNull

Import extension function untilNull (import org.awaitility.kotlin.untilNull) to wait until a value is null:

await untilNull { myDataRepository.findById("id") }

Likewise import extension function untilNotNull (import org.awaitility.kotlin.untilNotNull) to wait until a value is not null:

val data = await untilNotNull { myDataRepository.findById("id") }

Other Examples

As of version 3.1.2 you can also do for example:

await withPollInterval ONE_HUNDRED_MILLISECONDS ignoreException IllegalArgumentException::class untilAsserted  {
    assertThat(fakeRepository.value).isEqualTo(1)
}

or

await withAlias "Kotlin Test" ignoreExceptionsInstanceOf
      IllegalArgumentException::class withPollDelay ONE_HUNDRED_MILLISECONDS withPollInterval
      fibonacci().with().offset(1).and().timeUnit(MILLISECONDS) atLeast TWO_HUNDRED_MILLISECONDS atMost
      ONE_MINUTE untilCallTo { fakeRepository.value } matches { it == 1 }

Use the following Maven dependency:

<dependency>
    <groupId>org.awaitility</groupId>
    <artifactId>awaitility-kotlin</artifactId>
    <version>${awaitility.version}</version>
</dependency>

Example

Clone this wiki locally