struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Grobmeier <grobme...@gmail.com>
Subject Re: Junit Tests: Multiple Action calls lead to ClassCastException
Date Fri, 14 Oct 2011 16:23:20 GMT
Forget the latest comment:

On Fri, Oct 14, 2011 at 6:17 PM, Christian Grobmeier
<grobmeier@gmail.com> wrote:
> Another thing, even when I do that below, I only get the Result of the
> first Action. In my case i return json in each of the two actions. But
> I always get the first response. Probably something else needs to be
> reinitialized. Not sure what, tipps welcome

It works with the fix below. I somehow was to stupid with my new ide

Cheers


>
>
>
> On Fri, Oct 14, 2011 at 5:54 PM, Christian Grobmeier
> <grobmeier@gmail.com> wrote:
>> Guys,
>> its getting complicated. But I digged out a nasty thing in Junit
>> testing. I know whats wrong but looking for advise to fix the correct
>> place in Struts.
>>
>> Imagine:
>>
>> @RunWith(SpringJUnit4ClassRunner.class)
>> @ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
>> public class MyClass extends StrutsSpringJUnit4TestCase<MyAction> {
>>
>> @Test public void testMyStuff() {
>>       this.executeAction("/login.action");
>>       this.executeAction("/dostuff.action");
>>       Assert.assertEquals(1, this.getAction().methodFromMyAction());
>> }
>> }
>>
>>
>> So imagine what happens when I call getAction() which is MyAction for
>> the compiler?
>> Its a ClassCastException. Because actually its coming a LoginAction
>> from the first call back
>>
>> Here is how you can bring it to fly, in my special case:
>>
>> this.executeAction('/login.action');
>>
>> SessionMap session =
>> (SessionMap)ServletActionContext.getValueStack(request).getContext().get("session");
>> SessionUser sUser = (SessionUser)session.get("myuser");
>>
>> // Reinit request stuff
>> this.request = new MockHttpServletRequest();
>> this.response = new MockHttpServletResponse();
>> this.pageContext = new MockPageContext(servletContext, request, response);
>>
>> // Put stuff back into session what you need
>>  this.request.getSession().setAttribute("tabuser", sUser);
>>
>> // now it works:
>> this.executeAction("/dostuff.action");
>> Assert.assertEquals(1, this.getAction().methodFromMyAction());
>>
>>
>> So far so good - it seems like the first calls meta data like session
>> is somehow stored somewhere. To make it all work you'll need to reinit
>> the whole StrutsJUnit4TestCase mock objects - but then you'll loose
>> your session data you probably need for the second call.
>>
>> If we don't fix it, people can only use this for one test class = one
>> actions tests. But then how do you make tests for action flows?
>>
>> Now I need some good ideas how a potential test can look like- ideas welcome.
>>
>> In addition I would like to open an issue for that.
>>
>> Cheers
>> Christian
>>
>>
>> --
>> http://www.grobmeier.de
>>
>
>
>
> --
> http://www.grobmeier.de
>



-- 
http://www.grobmeier.de

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Mime
View raw message