Right, now I think I know what's going on, after I checked how I
mocked the request myself on the Gaelyk project
(http://gaelyk.appspot.com).
Here's an example:
private request = [
getRequestURI: { -> recorder << "req.getRequestURI"; uri },
getQueryString: { -> recorder << "req.getQueryString"; "" },
getRequestDispatcher: { String s -> recorder <<
"req.getRequestDispatcher"; requestDispatcher },
getHeader: { String h -> recorder << "req.getHeader"; dateAfter }
] as HttpServletRequest
See how I use the arrow?
Methods like getRequestURI() don't take any parameter, or getHeader()
takes a String parameter.
So it's important to have the right signature, ie. the right number of
parameters with their correct types as parameters of your closures.
Guillaume
On Mon, Jun 20, 2011 at 20:31, Stefan Gojan
Post by Stefan Gojan{'string'}] as HttpServletRequest
assert request.getHeader() == 'application/json'
HttpServletRequest.metaClass.getHeader = {'foo'}
assert request.getHeader() == 'foo'
assert request.getHeader() == 'foo'
| | |
string | false
application/json
I think it doesn't work, because request is a Proxy no
HttpServletRequest implementation. I looked at 'request' in the
{'string'}] as HttpServletRequest
assert request.getHeader() == 'application/json'
request.h.delegate['getHeader'] = {'foo'}
assert request.getHeader() == 'foo'
Is there a 'groovy' way to achive the same result?
Kind regards,
Stefan
On Mon, Jun 20, 2011 at 5:46 PM, Guillaume Laforge
Post by Guillaume LaforgeIt's not a problem with your getHeader method, it's because you have
to implement toString in your mock too.
(because as the error message shows: the toString method thew an
exception, because it's the default implementation)
On Mon, Jun 20, 2011 at 09:10, Stefan Gojan
request = [getHeader: {'application/json'}] as HttpServletRequest
assert request.getHeader() == 'application/json'
HttpServletRequest.metaClass.getHeader = {'foo'}
assert request.getHeader() == 'foo'
assert request.getHeader() == 'foo'
| | |
| | false
| application/json
java.lang.UnsupportedOperationException)
Regards,
Stefan
On Sun, Jun 19, 2011 at 11:09 PM, René Scheibe
Post by René ScheibePost by Stefan GojanSorry, you are right getTarget isn't a method of HttpServletRequest. I
mixed it up.
Just replace "getTarget" with "getHeader", how does it work when the
method exists?
def request = [getHeader: {return 'application/json'}] as HttpServletRequest
Calling request.getHeader('some name') returns 'application/json' as
expected when you stub the HttpServletRequest as above.
If you still struggle with it, just be more specific what is failing
(provide real source code and real error messages).
In case you want to stub other interfaces or classes, always keep in
mind that closures also have parameters which can be typed. If you don't
specify a parameter (as in your example) the implicit 'it' parameter is
assumed.
Regards,
René Scheibe
Post by Stefan GojanOn Sun, Jun 19, 2011 at 7:20 PM, René Scheibe
Post by René ScheibePost by Stefan GojanHi,
is there a way to change a mocked method when I use this code to mock a class?
// my mocked request
def request = [getTarget: {return 'foo/bar'}] as HttpServletRequest
There is no getTarget method in HttpServletRequest. So your code would
not stub it but create a new method. As HttpServletRequest is an
interface and the proxy generated via '...as HttpServletRequest' only
meets this interface your method cannot be called on request.
Post by Stefan GojanI tried to assign a new closure to
"HttpServeletRequest.metaClass.getTarget" which don't work :(
I hope someone can help me. I love groovy code much more for testing
like any other test framework out there ;)
Better don't globally change a so widely used interface. This could lead
to strange behavior.
Regards,
René Scheibe
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
--
http://en.gravatar.com/vollekannehoschi
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
--
http://en.gravatar.com/vollekannehoschi
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email