Let's say I have a Joiner
interface:
interface Joiner {
String join(List<String> l);
}
And two implementations of it:
class Java8Joiner implements Joiner {
@Override String join(List<String> l) { String.join("", l); }
}
class GroovyJoiner implements Joiner {
@Override String join(List<String> l) { l.join('') }
}
What is the best way to check that theses two implementation, or more, pass the same tests?
Basically, I want to run all the tests defined by JoinerSpecification with a Java8Joiner, then a GroovyJoiner, and so on...
class JoinerSpecification extends Specification {
@Subject
final def joiner = // Joiner instance here
def "Joining #list -> #expectedString"(List<String> list, String expectedString) {
expect:
joiner.join(list) == expectedString
where:
list | expectedString
[] | ''
['a'] | 'a'
['a', 'b'] | 'ab'
}
}
It is perfectly fine to refactor JoinerSpecification if needed.
My primary goals are:
- Maintainable test code (with as little boilerplate as possible)
- Clear error messages (which test of which implementation is failing)
- Easy to run from the IDE
Edit 1 (15/06/2015)
Reworded my question & added some details to make it clearer.
What would be very useful here is comparison chaining but, as far as I know it's not implemented in groovy, see here. Since
Subject
purpose according to the docs is pure informational I've come up with the following ideas:In the attempt 1 I'm just checking if every element on the list is equal to
expectedString
. It looks nice however on error it's not verbose enough. Any element may fail and there's no trace which one.The attempt 2 method makes use of
Set
. Since all the results should be equal there'll be only one element left in the collection. In prints verbose info on test failure.What also comes to my head is guava's
ComparisonChain
but no idea if it would be useful as well as it's another project dependency.