Skip to content

Instantly share code, notes, and snippets.

@ledoyen
Created June 20, 2018 14:21
Show Gist options
  • Save ledoyen/22fbe8a4a6e0e378935f84faae40f2e8 to your computer and use it in GitHub Desktop.
Save ledoyen/22fbe8a4a6e0e378935f84faae40f2e8 to your computer and use it in GitHub Desktop.
XML comparison with wildcards using XMLUnit
val diff = DiffBuilder.compare(expected).withTest(actual)
.withNodeMatcher(DefaultNodeMatcher(ElementSelectors.byNameAndAllAttributes, ElementSelectors.byName))
.ignoreWhitespace()
.ignoreComments()
.withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default, XmlWildcardDifferenceEvaluator()))
.checkForSimilar()
.build()
assertThat(diff.differences).isEmpty()
class XmlWildcardDifferenceEvaluator : DifferenceEvaluator {
override fun evaluate(comparison: Comparison?, outcome: ComparisonResult): ComparisonResult {
var wildcardOutcome = outcome
if (wildcardOutcome == ComparisonResult.DIFFERENT) {
if(ComparisonType.TEXT_VALUE == comparison?.type || ComparisonType.ATTR_VALUE == comparison?.type) {
if("*" == comparison.controlDetails.value) {
wildcardOutcome = ComparisonResult.SIMILAR
}
}
}
return wildcardOutcome
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment