You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Christian Grobmeier <gr...@gmail.com> on 2011/10/14 17:54:12 UTC

Junit Tests: Multiple Action calls lead to ClassCastException

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

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


Re: Junit Tests: Multiple Action calls lead to ClassCastException

Posted by Christian Grobmeier <gr...@gmail.com>.
On Fri, Oct 14, 2011 at 6:29 PM, Maurizio Cucchiara
<mc...@apache.org> wrote:
> Christian,
> I'm not sure I understand, Did you solve your issue?

No. Just the second message is crap. The first one still applies.
This is still true:

>>> On Fri, Oct 14, 2011 at 5:54 PM, Christian Grobmeier
>>> <gr...@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
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> For additional commands, e-mail: dev-help@struts.apache.org
>
>



-- 
http://www.grobmeier.de

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


Re: Junit Tests: Multiple Action calls lead to ClassCastException

Posted by Maurizio Cucchiara <mc...@apache.org>.
Christian,
I'm not sure I understand, Did you solve your issue?

Twitter     :http://www.twitter.com/m_cucchiara
G+          :https://plus.google.com/107903711540963855921
Linkedin    :http://www.linkedin.com/in/mauriziocucchiara

Maurizio Cucchiara



On 14 October 2011 18:23, Christian Grobmeier <gr...@gmail.com> wrote:
> Forget the latest comment:
>
> On Fri, Oct 14, 2011 at 6:17 PM, Christian Grobmeier
> <gr...@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
>> <gr...@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
>
>

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


Re: Junit Tests: Multiple Action calls lead to ClassCastException

Posted by Christian Grobmeier <gr...@gmail.com>.
Forget the latest comment:

On Fri, Oct 14, 2011 at 6:17 PM, Christian Grobmeier
<gr...@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
> <gr...@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


Re: Junit Tests: Multiple Action calls lead to ClassCastException

Posted by Christian Grobmeier <gr...@gmail.com>.
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



On Fri, Oct 14, 2011 at 5:54 PM, Christian Grobmeier
<gr...@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

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