Hi Java Gurus and AssertJ Gurus,
I would like to ask if there is a way in AssertJ to verify if an object exists from a list of Objects (e.g. ArrayList<TestObject> listOfTestObjects
). Where that particular object's field (getter method's return) value matches a pattern or regular expression pattern.
Please see example below for more details:
class TestObject {
private String stringValue;
public String getValue() {
return this.stringValue;
}
public void setValue(String newStringValue) {
this.stringValue = newStringValue;
}
}
ArrayList<TestObject> listOfTestObjects = new ArrayList<TestObject>();
// populate the list here...
assertThat(listOfTestObjects).extracting("value").containsAnElementWith("some regular expressions here...");
Please take note that I am not expecting "
...containsAnElementWith("some regular expressions here...");"
to be an existing method (which can actually be better if there is) but this can be a feature(s)/method(s) in assertj or junit that I can use to simplify unit test automation other than looping through one of them then doing a match.
Cheers
assertThat(listOfTestObjects).anyMatch(o -> o.getValue().matches(regex));