0

I'm unit testing a controller and currently I'm stuck with the method-call of "encodeAsJSON()" in a service (called by the controller).

I get the MissingMethodException, which I would expect as encodeAsJSON() is a Grails-Codec.

After a bit of searching the loadCodec-Method should do what I want.

But I can't import it, because my IDE (SpringSource-ToolSuite) can't find it.

Error:

import org.codehaus.groovy.grails.plugins.codecs.JSONCodec

Everything works:

import org.codehaus.groovy.grails.plugins.codecs.HTMLCodec

But I don't need the HTMLCodec and the documentation doesn't list the JSONCodec.

The method itself is still working (running the application), but I don't know how to unit test it. Any ideas? Am I missing something obvious?

4

1 回答 1

2

Well, further searching and trying got me the answer:

Map.metaClass.encodeAsJSON = { -> delegate }

The example I used was

String.metaClass.encodeAsSha1 = { -> delegate }

(Source)

String didn't work - of course as I'm using a Map, which should be converted to JSON.

I think you have to "mock" (is this real mocking?) it on the Groovy-Interface of the corresponding datatype.

Hope this helps somebody else

于 2012-07-12T19:39:41.123 回答