简体   繁体   中英

AssertJ: A way to check for an object's field value if it contains a particular pattern (regex)

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 (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM