Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HttpServletRequestSimulator throws classcast exception 1

Status
Not open for further replies.

PPJOHNSON

Programmer
Dec 22, 2003
9
AU
Hi I am working with automated testing using CactusTestcase from sourceforge.net. I am having a problem I want to set the request method to POST from my testcase.Since there is no setMethod() is in the HttpServletRequest I ahve to use HttpServletRequestSimulator class, but on runtime it throwing classcast exception from the strutsRequestWrapper class. Anybody got any idea , please help.
Johnson
 
Can you show the error and the code that is throwing it please
 
Hi
HttpServletRequestSimulator request =(HttpServletRequestSimulator)getRequest();
this is what I done in my code.
My class exytends CactusStrutsTestCase.
and this is the exception.

java.lang.ClassCastException: servletunit.struts.StrutsRequestWrapper
at au.com.qtech.coreweb.unittests.ModelAddTest.testLogon(ModelAddTest.java:41)
at java.lang.reflect.Method.invoke(Native Method)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at org.apache.cactus.ServletTestCase.runCactusTest(ServletTestCase.java:295)
at org.apache.cactus.ServletTestCase.runBare(ServletTestCase.java:263)
at org.apache.cactus.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:156)
at org.apache.cactus.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:130)
at org.apache.cactus.server.AbstractWebTestController.handleRequest_aroundBody1$advice(AbstractWebTestController.java:158)
at org.apache.cactus.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java)
at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(ServletTestRedirector.java:138)
at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody3$advice(ServletTestRedirector.java:158)
at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java)
at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody0(ServletTestRedirector.java:109)
at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody1$advice(ServletTestRedirector.java:158)
at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:948)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:530)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:176)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:79)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:201)
at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:610)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:431)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:593)

The exception generated from the above line of code
Thanks
 
So in this line
Code:
HttpServletRequestSimulator request =(HttpServletRequestSimulator)getRequest();

What object does the method "getRequest()" return ?
 
it returning servletunit.struts.StrutsRequestWrapper
type this class and and the simulator have the implementation of HttpServletRequest.
Thanks
 
Hi , I checked the MockStrutcase code , it is creating the Simulator request object in MockStrutsTestCase class .I am clarified about casting. so now I want a help to set the requestMethod to Post, how can I do it from my test class.
Thanks
 
I guess using the method :

Code:
HttpServletRequestSimulator.setMethod(HttpServletRequestSimulator.POST);
 
It is fine when I am exteding from MockStrutsTestCase .But I want to do some In-container testing using CactusStrutsTestCase .I want to do the testing with Post method in my code like Adding an item to database when user clicking Add button.It is happening in Post method. In execute method it is looking for request.getMethod() then it is caliing Post or Get. SO I want to set the request method either Post or Get according to the situation in my testcase. How can I do that?
Please help.
 
One more problem SEDJ
now I am getting this exception when runnung MockstrutsTestcase
Testcase: testLogon(au.com.qtech.coreweb.unittests.ModelAddTest1): Caused an ERROR
[junit] null
[junit] java.lang.ClassFormatError
[junit] at java.lang.Throwable.<init>(Throwable.java)
[junit] at java.lang.Throwable.<init>(Throwable.java)
[junit] at java.lang.ClassFormatError.<init>(ClassFormatError.java:52)
[junit] at java.lang.ClassLoader.defineClassImpl(Native Method)
[junit] at java.lang.ClassLoader.defineClass(ClassLoader.java)
[junit] at java.lang.reflect.AccessibleObject.invokeL(AccessibleObject.java:207)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java)
[junit] at org.apache.commons.digester.Digester.getXMLReader(Digester.java:905)
[junit] at org.apache.commons.digester.Digester.parse(Digester.java:1567)
[junit] at org.apache.struts.action.ActionServlet.initServlet(ActionServlet.java:1433)
[junit] at org.apache.struts.action.ActionServlet.init(ActionServlet.java:466)
[junit] at javax.servlet.GenericServlet.init(GenericServlet.java:258)
[junit] at servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:279)
[junit] at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:332)
[junit] at au.com.qtech.coreweb.unittests.ModelAddTest1.testLogon(ModelAddTest1.java:79)
[junit] at java.lang.reflect.AccessibleObject.invokeV(AccessibleObject.java:199)
[junit] at org.eclipse.ant.internal.core.ant.InternalAntRunner.run(InternalAntRunner.java:571)
[junit] at org.eclipse.ant.internal.core.ant.InternalAntRunner.run(InternalAntRunner.java:367)
[junit] at java.lang.reflect.AccessibleObject.invokeV(AccessibleObject.java:199)
[junit] at org.eclipse.ant.core.AntRunner.run(AntRunner.java:335)
[junit] at org.eclipse.ui.externaltools.internal.ant.launchConfigurations.AntLaunchDelegate$1.run(AntLaunchDelegate.java:172)
[junit] at java.lang.Thread.run(Thread.java:801)

ANy idea. can you help me?
 
The API says :

&quot;Thrown when the Java Virtual Machine attempts to read a class file and determines that the file is malformed or otherwise cannot be interpreted as a class file. &quot;

In real terms this means that you have probably compiled the code on one version of SDK, and tried to run it on a lower version JVM. Is this the case ?
 
Hi Sedj,
Now I am not getting this exception, Have you got any Idea about my previous question about setting method to post in CatusTestCase.
Thanks for the reply.
 
Looking at the documentation, CactusStrutsTestCase appears to be used for testing Struts Action classes - which don't care whether the http method is GET or POST - so I am confused why you need to bother ?

To be honest, I spent 4 months programming Struts, and found it in practice to be a bloated and unwieldy framework , which appears much better on paper than it does in the real world. I imagine the unit tests are equally unwieldy ! I've found the best way to test a web site is to hit it as hard as you can with a load simulater (an OK free one is OpenSTA - you traverse through a website in a browser, and it records what you do - you then replay this lots to simulate multiple users)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top