This code works fine when I run it in Grails.
String getLoginToken() {
generatePassword()
passwordExpired = false
[email, password].join(',').encodeAsBase64()
}
However, this Spock test fails
def "test getLoginToken"() {
setup:
String email = "[email protected]"
Person person = new Person(email: email)
when:
String token = person.getLoginToken()
then:
token.decodeBase64() == "$email,$person.password"
}
with the following exception
| Failure: test getLoginToken(com.campuscardtools.myphotoid.PersonSpec)
| groovy.lang.MissingMethodException: No signature of method: java.lang.String.encodeAsBase64() is applicable for argument types: () values: []
Possible solutions: decodeBase64()
at com.campuscardtools.myphotoid.Person$$EPFScS6i.getLoginToken(Person.groovy:140)
at com.campuscardtools.myphotoid.PersonSpec.test getLoginToken(PersonSpec.groovy:68)
My understanding is that Groovy provides the encodeAsBase64() on the String class (see: http://mrhaki.blogspot.com/2009/11/groovy-goodness-base64-encoding.html), so why doesn't this work in the unit test?
Rather than
You need
Without the 'As'