Search code examples
kotlinassertionkotlintestkotest

Assert that every object property matches given predicate in kotlin test


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" }

Solution

  • What about using an inspector.

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

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