Assert that every object property matches given predicate in kotlin test

2.2k Views Asked by At

I have a collection of objects:

data class WeatherForecast(
    val city: String,
    val forecast: String
    // ...
)

I would like to test that each and every item matches given predicate on field.

Is there any assertion in kotlintest assertions that will allow me to do so?

Something like:

 forecasts.eachItemshouldMatch{ it.forecast == "SUNNY" }
2

There are 2 best solutions below

0
sksamuel On BEST ANSWER

What about using an inspector.

list.forAll {
  it.forecast shouldBe "SUNNY"
}

https://kotest.io/docs/assertions/inspectors.html

2
Schottky On

You can simply use the all function; i.e.:

forecasts.all { it.forecast == "SUNNY" }