Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mathifonseca/56607b2917527f2acd66 to your computer and use it in GitHub Desktop.
Save mathifonseca/56607b2917527f2acd66 to your computer and use it in GitHub Desktop.
GRAILS - ToString of a null param

I hate it. I simply hate it.

If a Grails parameter is null and you call toString() on it, it doesn't fail with a good old NullPointerException like I expected. Instead, what you get is a String with value 'null'.

The only way to get a null value when the param is null, is to use the Groovy Safe Navigation operator.

Therefore:

given:
    params.some = null
when:
    controller.index()
then:
    notThrown NullPointerException
    params.some.toString() == 'null'
    params.some?.toString() == null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment