Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
Is there a Kotest assertion to test if a list contains some element with a given property?

I have two objects that have some features in common that I would like to compare

data class Pet(val colour: String, val owner: Human, val legCount: Int)
data class Car(val colour: String, val owner: Human, val isElectric: Boolean)

I would like to assert that some element in a List<Car> contains an element with the same colour and owner as a given Pet.

Here's a fake code example to illustrate what I'm trying to do:

cars.containsElementSatisfying { car ->
    pet.colour shouldBe car.colour
    pet.owner shouldBe car.owner
}

How might I do this using kotest? I can accomplish the desired functionality using assertJ's anySatisfy assertion.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I found the solution. The "Inspectors" kotest library contains a number of functions similar to assertJ's anySatisfy assertion, in particular the collection functions forAtLeastOne and forAtLeast(k)

Here's example usage:

cars.forAtLeastOne { car -> 
    pet.colour shouldBe car.colour
    pet.owner shouldBe car.owner
}

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!