I have a list containing params with one value a Gstring. When I compare via Hamcrest I get String isn't GString failure even though contents match when compared as Strings
What's the right approach to get Hamcrest to resolve Gstring into string during its comparison? A workaround is to call .toString()
on my actual Gstring as I add it to the list but that's not ideal
@Test
def stackOverflow() {
given:
// values
def v1 = '01'
def v2 = '02'
// a groovy string containing them
def param = "${v1}:${v2}"
// a matcher using a concrete string
def matcher = Matchers.equalTo("01:02")
when:
// add actual into a list
def mylist = [param]
then:
// check that actual Gstring matches expected String and fail
that mylist, Matchers.hasItem(matcher)
}
ondition not satisfied:
that mylist, Matchers.hasItem(matcher)
| | |
| [01:02] class org.hamcrest.Matchers
false
Expected: a collection containing "01:02"
but: mismatches were: [was <01:02>]
Just use String
instead of def
when declaring the variable.
import static spock.util.matcher.HamcrestSupport.that
import static org.hamcrest.Matchers.*
import spock.lang.*
class ASpec extends Specification {
def stackOverflow() {
given:
// values
def v1 = '01'
def v2 = '02'
// a groovy string containing them
String param = "${v1}:${v2}"
// a matcher using a concrete string
def matcher = equalTo("01:02")
when:
// add actual into a list
def mylist = [param]
then:
// check that actual Gstring matches expected String and fail
that mylist, hasItem(matcher)
}
}
Try it in the Groovy Web Console
p.s. the @Test
doesn't belong to Spock and should be removed.