Better way to assert correct return values in Groovy
You can use with to do things inline without declaring another variable:

List list = [
  [first: "foo", last: "bar"],
  [first: "baz", last: "qux"],
  [first: "baz", last: "bar"]
]

List findDuplicateElements(myObjects) {
   myObjects
   .groupBy { it.last }
   .findAll { it.value.size() &gt; 1 }
   .collect { it.value }
   .flatten()
   .with {
     assert it.size() != null
     it
   }
}

assert findDuplicateElements(list).size() == 2