I am trying to write a custom domain related assert/matcher in spock or hamcrest, but I am not sure how to proceed.
I tried writing a custom Matcher in hamcrest but so far that has only led me to a partial solution.
I am looking for some guidance as to what the proper course in this scenario would be.
Domain Objects:
- ResultMap has an object Map < ILine, IResult > - There is one INodeResult associated with each ILine.
- IResult has 4 (Google Guava) multimap objects that need to verified.
What I would like to do in my spock test is something like:
expect:
that actualResultMap, matchesInAnyOrder(expectedResultMap)
or
that actualResultMap, matches(expectedResultMap) // Will only match if everything is in the same order.
The internal code will then evaluate each entry and do the appropriate tests on the inner objects as well.
So far I managed to write a part of the code that evaluates one set of multimap, but I am not sure how to chain my tests.
Custom Matcher:
package com.ps.DE.Test.CustomMatcher
import org.hamcrest.BaseMatcher
class MultimapMatcher {
/**
* Checks all the entries in a Multimap with another
* @param expected
* @return Shows the failure only if the entries do not match or are not in the same order
*/
static hasAllInOrder(final com.google.common.collect.Multimap expected){
[
matches: { actual ->
for(key in actual.keySet()){
if (actual.get(key) != expected.get(key)){
return false
}
}
return true
},
describeTo: { description ->
description.appendText("MultiMap entries to be ${expected}")
},
describeMismatch: { actual, description ->
description.appendText("were ${actual}")
}
] as BaseMatcher
}
/**
* Checks all the entries in a Multimap with another
* @param expected
* @return Shows the failure only if the entries do not match
*/
static hasAllInAnyOrder(final com.google.common.collect.Multimap expected){
[
matches: { actual ->
for(key in actual.keySet()){
if (!actual.get(key).containsAll(expected.get(key))) {
return false
}
}
return true
},
describeTo: { description ->
description.appendText("MultiMap entries to be ${expected}")
},
describeMismatch: { actual, description ->
description.appendText("were ${actual}")
}
] as BaseMatcher
}
}
Testing the custom Matcher:
package com.ps.DE.Test.CustomMatcher
import com.google.common.collect.ArrayListMultimap
import com.google.common.collect.Multimap
import spock.lang.Specification
import static com.ps.DE.Test.CustomMatcher.MultimapMatcher.hasAllInAnyOrder
import static com.ps.DE.Test.CustomMatcher.MultimapMatcher.hasAllInOrder
import static org.hamcrest.Matchers.not
import static spock.util.matcher.HamcrestSupport.that
class MultimapMatcherSpec extends Specification {
def "Test hasAllInOrder"() {
def actual = ArrayListMultimap.create();
// Adding some key/value
actual.put "Fruits", "Apple"
actual.put "Fruits", "Banana"
actual.put "Fruits", "Pear"
actual.put "Vegetables", "Carrot"
Multimap<String, String> expected = ArrayListMultimap.create();
// Adding some key/value
expected.put("Fruits", "Apple");
expected.put("Fruits", "Banana");
expected.put("Fruits", "Pear");
expected.put("Vegetables", "Carrot");
expect:
that actual, hasAllInAnyOrder(expected)
that actual, hasAllInOrder(expected)
}
def "Test hasAllInAnyOrder"() {
Multimap<String, String> actual = ArrayListMultimap.create();
// Adding some key/value
actual.put("Fruits", "Apple");
actual.put("Fruits", "Banana");
actual.put("Fruits", "Pear");
actual.put("Vegetables", "Carrot");
Multimap<String, String> expected = ArrayListMultimap.create();
// Adding some key/value
expected.put("Fruits", "Banana");
expected.put("Fruits", "Apple");
expected.put("Fruits", "Pear");
expected.put("Vegetables", "Carrot");
expect:
that actual, hasAllInAnyOrder(expected)
that actual, not(hasAllInOrder(expected))
}
}
Any help or guidance will be greatly appreciated.