Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails String.encodeAsBase64() Fails in Spock Tests

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?

like image 228
SGT Grumpy Pants Avatar asked Nov 28 '25 06:11

SGT Grumpy Pants


2 Answers

Rather than

"Blah".encodeAsBase64()

You need

"Blah".encodeBase64()

Without the 'As'

like image 156
tim_yates Avatar answered Nov 30 '25 00:11

tim_yates


You could also include a mockCodec for the method you're using.

and: "add the Base64 codec"
    mockCodec(org.codehaus.groovy.grails.plugins.codecs.Base64Codec)
like image 42
Jeff Blaisdell Avatar answered Nov 30 '25 00:11

Jeff Blaisdell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!