Background
org.dbunit.assertion.comparer.value has ValueComparer implementations for equality, ordering, numeric/timestamp tolerance, null checks, and literal substring containment (IsActualContainingExpectedStringValueComparer), but none for matching a column value against a regular expression.
Regex matching is the natural fit for verifying values that are correct in shape but not in exact content:
- database-generated surrogate keys (
[0-9]+)
- UUID / GUID columns stored as text
- formatted identifiers (order numbers, SKUs, account numbers)
- timestamps or dates rendered into a text column
- any value where only the format, not the exact content, is under the test's control
Today each project has to write its own ValueComparer for this.
Proposed design
Add org.dbunit.assertion.comparer.value.RegularExpressionValueComparer:
- The expected value (from the expected dataset) is the regular expression; the actual value from the database, converted to a
String, is tested against it. Same "expected value carries the comparison criterion" model as IsActualContainingExpectedStringValueComparer.
- Matches when the pattern matches the entire actual value (
java.util.regex.Matcher.matches()), consistent with String.matches(), AssertJ .matches(), and Hamcrest matchesPattern(). A caller wanting a partial match wraps the pattern in .*.
- Null handling mirrors the sibling comparers: both null match; exactly one null does not.
- An invalid regex in the expected value throws
DatabaseUnitException (wrapping PatternSyntaxException) with row/column context, mirroring how IsActualEqualToExpectedJsonValueComparer reports an unparseable expected value.
- No new dependency (
java.util.regex is part of the JDK), so it is also exposed as a ValueComparers constant, unlike the Jackson-backed JSON comparer.
Docs
Update datacomparisons/valuecomparer.adoc and add a changes.xml entry.
Background
org.dbunit.assertion.comparer.valuehasValueComparerimplementations for equality, ordering, numeric/timestamp tolerance, null checks, and literal substring containment (IsActualContainingExpectedStringValueComparer), but none for matching a column value against a regular expression.Regex matching is the natural fit for verifying values that are correct in shape but not in exact content:
[0-9]+)Today each project has to write its own
ValueComparerfor this.Proposed design
Add
org.dbunit.assertion.comparer.value.RegularExpressionValueComparer:String, is tested against it. Same "expected value carries the comparison criterion" model asIsActualContainingExpectedStringValueComparer.java.util.regex.Matcher.matches()), consistent withString.matches(), AssertJ.matches(), and HamcrestmatchesPattern(). A caller wanting a partial match wraps the pattern in.*.DatabaseUnitException(wrappingPatternSyntaxException) with row/column context, mirroring howIsActualEqualToExpectedJsonValueComparerreports an unparseable expected value.java.util.regexis part of the JDK), so it is also exposed as aValueComparersconstant, unlike the Jackson-backed JSON comparer.Docs
Update
datacomparisons/valuecomparer.adocand add achanges.xmlentry.