A
andyS
Guest
Hi all,
I have a service that works with the Request and the Session
class myService{
def boolean detectSomething(){
GrailsWebRequest request = RequestContextHolder.currentRequestAttributes()
GrailsHttpSession session = request.session
// do some stuff ..... with the request and the session
}
}
then I have a Unit test and tried
void testMyServiceDetectSomething() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("user-agent",userAgentForTesting_HTC_Hero)
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
def something =myService.detectSomething();
// do some stuff
}
If I start the test, I receive this Class-Cast-error
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'org.springframework.mock.web.MockHttpServletRequest@6f8495' with class 'org.springframework.web.context.request.ServletRequestAttributes' to class 'org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:360)
How can I unit test the Service avoiding this class-cast-Error, I need the GrailsWebRequestClass in myService, can I inject this maybe in some way?
Thanks a lot for helping
Andy
I have a service that works with the Request and the Session
class myService{
def boolean detectSomething(){
GrailsWebRequest request = RequestContextHolder.currentRequestAttributes()
GrailsHttpSession session = request.session
// do some stuff ..... with the request and the session
}
}
then I have a Unit test and tried
void testMyServiceDetectSomething() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("user-agent",userAgentForTesting_HTC_Hero)
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
def something =myService.detectSomething();
// do some stuff
}
If I start the test, I receive this Class-Cast-error
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'org.springframework.mock.web.MockHttpServletRequest@6f8495' with class 'org.springframework.web.context.request.ServletRequestAttributes' to class 'org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:360)
How can I unit test the Service avoiding this class-cast-Error, I need the GrailsWebRequestClass in myService, can I inject this maybe in some way?
Thanks a lot for helping
Andy