You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Joey Watson <jo...@gmail.com> on 2008/02/01 09:28:34 UTC

ActionContext.getContext().getSession return null when I test Struts2 action by JUnit.

Hi everybody.

I want to test a action class (Struts2) by JUnit. and then method in
this action need a value in session, then when I run my JUnit test
class,  ActionContext.getContext().getSession return null. (not
ActionContext.getContext().getSession().get("XXX") return null).
this action class is working fine in browser.


thanks for any help

Joey

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


Re: ActionContext.getContext().getSession return null when I test Struts2 action by JUnit.

Posted by Laurie Harper <la...@holoweb.net>.
For completeness: you could also re-work your action to implement 
SessionAware instead of using ActionContext.getContext().getSession(). 
Unit testing would then simply involve calling setSession() on the 
action under test. The SessionAware interface exists, in part, to make 
testing easier by decoupling your actions from their execution context.

L.

Joey wrote:
> I found it . like this.
> 
>         Map param = new HashMap();
>         ActionContext.getContext().setSession(param);
> 
> Thanks for your help
> 
> On Feb 1, 2008 4:53 PM, Joey <jo...@gmail.com> wrote:
>> Thanks,  I just checked testing class in Struts2 package,  but I am a
>> newbie for Struts2. I cannot understant Struts2 very well, so if you
>> don't mind, pls tell me which testing class I can learn from.
>>
>> Thanks again
>>
>>
>> On Feb 1, 2008 4:31 PM, Nils-Helge Garli Hegvik <ni...@gmail.com> wrote:
>>> You need to initialize the ActionContext in your unit tests. Take a
>>> look at the Struts 2 tests how it is solved there.
>>>
>>> Nils-H
>>>
>>>
>>> On Feb 1, 2008 9:28 AM, Joey Watson <jo...@gmail.com> wrote:
>>>> Hi everybody.
>>>>
>>>> I want to test a action class (Struts2) by JUnit. and then method in
>>>> this action need a value in session, then when I run my JUnit test
>>>> class,  ActionContext.getContext().getSession return null. (not
>>>> ActionContext.getContext().getSession().get("XXX") return null).
>>>> this action class is working fine in browser.
>>>>
>>>>
>>>> thanks for any help
>>>>
>>>> Joey
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>>> For additional commands, e-mail: user-help@struts.apache.org
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>


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


Re: ActionContext.getContext().getSession return null when I test Struts2 action by JUnit.

Posted by Juanjo Cuadrado <jj...@gmail.com>.
yes, but I prefer not to use SessionAware because (I think) that this
complicates testing


2009/8/3 Nils-Helge Garli Hegvik <ni...@gmail.com>

> If I have to, then I just do new ActionContext() and
> ActionContext.setContext(). But then again, if I did, I would consider
> refactoring it into using SessionAware if it was possible, since it's
> a lot more test friendly.
>
> Nils-H
>
> On Mon, Aug 3, 2009 at 1:37 PM, Juanjo Cuadrado<jj...@gmail.com>
> wrote:
> > Uummm... I haven't looked at the source, but I looked the
> documentation...
> > And I could see that the getContext of ActionContext never returns null
> >
> >
> http://struts.apache.org/2.1.6/struts2-core/apidocs/com/opensymphony/xwork2/ActionContext.html#getContext()<http://struts.apache.org/2.1.6/struts2-core/apidocs/com/opensymphony/xwork2/ActionContext.html#getContext%28%29>
> >
> > Well, how have I to do the test now? How have I to create the
> ActionContext?
> > is it a bug?
> >
> >
> > 2009/8/3 Nils-Helge Garli Hegvik <ni...@gmail.com>
> >
> >> Well, if that was a question, then I didn't see it....
> >>
> >> I would certainly prefer implementing SessionAware, if it's an action
> >> you're testing. But if you really need to set up the ActionContext you
> >> have to create one first and the "activate" it. If you look at the
> >> xwork source code for 2.0.5 you can see that the ActionContext is
> >> lazily created for you when getContext() is invoked, whereas in 2.1.2,
> >> the context is no longer being created if it does not exist.
> >>
> >> Nils-H
> >>
> >> On Mon, Aug 3, 2009 at 1:06 PM, Juanjo Cuadrado<jj...@gmail.com>
> >> wrote:
> >> > And... what is the problem??? I have the same problem with junit test
> and
> >> > actions. No matter how long ago the comment was written. He might have
> >> found
> >> > out already, but I not and he not has written the solution here.
> >> >
> >> > But I wan to say a thing... When you does
> >> > "ActionContext.getContext().setSession(param)" with xwork-2.1.2, you
> get
> >> a
> >> > NullPointerException, but if you do it with xwork-2.0.5, it works.
> >> >
> >> > If any know what is happening, I would be extremely grateful.
> >> >
> >> >
> >> > 2009/8/3 Nils-Helge Garli Hegvik <ni...@gmail.com>
> >> >
> >> >> He might have found out already, considering this was posted 1,5
> years
> >> >> ago...
> >> >>
> >> >> Nils-H
> >> >>
> >> >> On Mon, Aug 3, 2009 at 12:44 PM, Juanjo Cuadrado<
> jjcuadrado@gmail.com>
> >> >> wrote:
> >> >> > Hi,
> >> >> >
> >> >> > That is wrong... if you do this, throws a NullPointerException...
> >> >> >
> >> >> >
> >> >> > 2008/2/1 Joey <jo...@gmail.com>
> >> >> >
> >> >> >> I found it . like this.
> >> >> >>
> >> >> >>        Map param = new HashMap();
> >> >> >>        ActionContext.getContext().setSession(param);
> >> >> >>
> >> >> >> Thanks for your help
> >> >> >>
> >> >> >> On Feb 1, 2008 4:53 PM, Joey <jo...@gmail.com> wrote:
> >> >> >> > Thanks,  I just checked testing class in Struts2 package,  but I
> am
> >> a
> >> >> >> > newbie for Struts2. I cannot understant Struts2 very well, so if
> >> you
> >> >> >> > don't mind, pls tell me which testing class I can learn from.
> >> >> >> >
> >> >> >> > Thanks again
> >> >> >> >
> >> >> >> >
> >> >> >> > On Feb 1, 2008 4:31 PM, Nils-Helge Garli Hegvik <
> nilsga@gmail.com>
> >> >> >> wrote:
> >> >> >> > > You need to initialize the ActionContext in your unit tests.
> Take
> >> a
> >> >> >> > > look at the Struts 2 tests how it is solved there.
> >> >> >> > >
> >> >> >> > > Nils-H
> >> >> >> > >
> >> >> >> > >
> >> >> >> > > On Feb 1, 2008 9:28 AM, Joey Watson <
> joey.information@gmail.com>
> >> >> >> wrote:
> >> >> >> > > > Hi everybody.
> >> >> >> > > >
> >> >> >> > > > I want to test a action class (Struts2) by JUnit. and then
> >> method
> >> >> in
> >> >> >> > > > this action need a value in session, then when I run my
> JUnit
> >> test
> >> >> >> > > > class,  ActionContext.getContext().getSession return null.
> (not
> >> >> >> > > > ActionContext.getContext().getSession().get("XXX") return
> >> null).
> >> >> >> > > > this action class is working fine in browser.
> >> >> >> > > >
> >> >> >> > > >
> >> >> >> > > > thanks for any help
> >> >> >> > > >
> >> >> >> > > > Joey
> >> >> >> > > >
> >> >> >> > > >
> >> >> ---------------------------------------------------------------------
> >> >> >> > > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> >> >> > > > For additional commands, e-mail:
> user-help@struts.apache.org
> >> >> >> > > >
> >> >> >> > > >
> >> >> >> > >
> >> >> >> > >
> >> >> ---------------------------------------------------------------------
> >> >> >> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> >> >> > > For additional commands, e-mail: user-help@struts.apache.org
> >> >> >> > >
> >> >> >> > >
> >> >> >> >
> >> >> >>
> >> >> >>
> ---------------------------------------------------------------------
> >> >> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> >> >> For additional commands, e-mail: user-help@struts.apache.org
> >> >> >>
> >> >> >>
> >> >> >
> >> >>
> >> >> ---------------------------------------------------------------------
> >> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> >> For additional commands, e-mail: user-help@struts.apache.org
> >> >>
> >> >>
> >> >
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >>
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: ActionContext.getContext().getSession return null when I test Struts2 action by JUnit.

Posted by Nils-Helge Garli Hegvik <ni...@gmail.com>.
If I have to, then I just do new ActionContext() and
ActionContext.setContext(). But then again, if I did, I would consider
refactoring it into using SessionAware if it was possible, since it's
a lot more test friendly.

Nils-H

On Mon, Aug 3, 2009 at 1:37 PM, Juanjo Cuadrado<jj...@gmail.com> wrote:
> Uummm... I haven't looked at the source, but I looked the documentation...
> And I could see that the getContext of ActionContext never returns null
>
> http://struts.apache.org/2.1.6/struts2-core/apidocs/com/opensymphony/xwork2/ActionContext.html#getContext()
>
> Well, how have I to do the test now? How have I to create the ActionContext?
> is it a bug?
>
>
> 2009/8/3 Nils-Helge Garli Hegvik <ni...@gmail.com>
>
>> Well, if that was a question, then I didn't see it....
>>
>> I would certainly prefer implementing SessionAware, if it's an action
>> you're testing. But if you really need to set up the ActionContext you
>> have to create one first and the "activate" it. If you look at the
>> xwork source code for 2.0.5 you can see that the ActionContext is
>> lazily created for you when getContext() is invoked, whereas in 2.1.2,
>> the context is no longer being created if it does not exist.
>>
>> Nils-H
>>
>> On Mon, Aug 3, 2009 at 1:06 PM, Juanjo Cuadrado<jj...@gmail.com>
>> wrote:
>> > And... what is the problem??? I have the same problem with junit test and
>> > actions. No matter how long ago the comment was written. He might have
>> found
>> > out already, but I not and he not has written the solution here.
>> >
>> > But I wan to say a thing... When you does
>> > "ActionContext.getContext().setSession(param)" with xwork-2.1.2, you get
>> a
>> > NullPointerException, but if you do it with xwork-2.0.5, it works.
>> >
>> > If any know what is happening, I would be extremely grateful.
>> >
>> >
>> > 2009/8/3 Nils-Helge Garli Hegvik <ni...@gmail.com>
>> >
>> >> He might have found out already, considering this was posted 1,5 years
>> >> ago...
>> >>
>> >> Nils-H
>> >>
>> >> On Mon, Aug 3, 2009 at 12:44 PM, Juanjo Cuadrado<jj...@gmail.com>
>> >> wrote:
>> >> > Hi,
>> >> >
>> >> > That is wrong... if you do this, throws a NullPointerException...
>> >> >
>> >> >
>> >> > 2008/2/1 Joey <jo...@gmail.com>
>> >> >
>> >> >> I found it . like this.
>> >> >>
>> >> >>        Map param = new HashMap();
>> >> >>        ActionContext.getContext().setSession(param);
>> >> >>
>> >> >> Thanks for your help
>> >> >>
>> >> >> On Feb 1, 2008 4:53 PM, Joey <jo...@gmail.com> wrote:
>> >> >> > Thanks,  I just checked testing class in Struts2 package,  but I am
>> a
>> >> >> > newbie for Struts2. I cannot understant Struts2 very well, so if
>> you
>> >> >> > don't mind, pls tell me which testing class I can learn from.
>> >> >> >
>> >> >> > Thanks again
>> >> >> >
>> >> >> >
>> >> >> > On Feb 1, 2008 4:31 PM, Nils-Helge Garli Hegvik <ni...@gmail.com>
>> >> >> wrote:
>> >> >> > > You need to initialize the ActionContext in your unit tests. Take
>> a
>> >> >> > > look at the Struts 2 tests how it is solved there.
>> >> >> > >
>> >> >> > > Nils-H
>> >> >> > >
>> >> >> > >
>> >> >> > > On Feb 1, 2008 9:28 AM, Joey Watson <jo...@gmail.com>
>> >> >> wrote:
>> >> >> > > > Hi everybody.
>> >> >> > > >
>> >> >> > > > I want to test a action class (Struts2) by JUnit. and then
>> method
>> >> in
>> >> >> > > > this action need a value in session, then when I run my JUnit
>> test
>> >> >> > > > class,  ActionContext.getContext().getSession return null. (not
>> >> >> > > > ActionContext.getContext().getSession().get("XXX") return
>> null).
>> >> >> > > > this action class is working fine in browser.
>> >> >> > > >
>> >> >> > > >
>> >> >> > > > thanks for any help
>> >> >> > > >
>> >> >> > > > Joey
>> >> >> > > >
>> >> >> > > >
>> >> ---------------------------------------------------------------------
>> >> >> > > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> >> > > > For additional commands, e-mail: user-help@struts.apache.org
>> >> >> > > >
>> >> >> > > >
>> >> >> > >
>> >> >> > >
>> >> ---------------------------------------------------------------------
>> >> >> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> >> > > For additional commands, e-mail: user-help@struts.apache.org
>> >> >> > >
>> >> >> > >
>> >> >> >
>> >> >>
>> >> >> ---------------------------------------------------------------------
>> >> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> >> For additional commands, e-mail: user-help@struts.apache.org
>> >> >>
>> >> >>
>> >> >
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> For additional commands, e-mail: user-help@struts.apache.org
>> >>
>> >>
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>

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


Re: ActionContext.getContext().getSession return null when I test Struts2 action by JUnit.

Posted by Juanjo Cuadrado <jj...@gmail.com>.
Uummm... I haven't looked at the source, but I looked the documentation...
And I could see that the getContext of ActionContext never returns null

http://struts.apache.org/2.1.6/struts2-core/apidocs/com/opensymphony/xwork2/ActionContext.html#getContext()

Well, how have I to do the test now? How have I to create the ActionContext?
is it a bug?


2009/8/3 Nils-Helge Garli Hegvik <ni...@gmail.com>

> Well, if that was a question, then I didn't see it....
>
> I would certainly prefer implementing SessionAware, if it's an action
> you're testing. But if you really need to set up the ActionContext you
> have to create one first and the "activate" it. If you look at the
> xwork source code for 2.0.5 you can see that the ActionContext is
> lazily created for you when getContext() is invoked, whereas in 2.1.2,
> the context is no longer being created if it does not exist.
>
> Nils-H
>
> On Mon, Aug 3, 2009 at 1:06 PM, Juanjo Cuadrado<jj...@gmail.com>
> wrote:
> > And... what is the problem??? I have the same problem with junit test and
> > actions. No matter how long ago the comment was written. He might have
> found
> > out already, but I not and he not has written the solution here.
> >
> > But I wan to say a thing... When you does
> > "ActionContext.getContext().setSession(param)" with xwork-2.1.2, you get
> a
> > NullPointerException, but if you do it with xwork-2.0.5, it works.
> >
> > If any know what is happening, I would be extremely grateful.
> >
> >
> > 2009/8/3 Nils-Helge Garli Hegvik <ni...@gmail.com>
> >
> >> He might have found out already, considering this was posted 1,5 years
> >> ago...
> >>
> >> Nils-H
> >>
> >> On Mon, Aug 3, 2009 at 12:44 PM, Juanjo Cuadrado<jj...@gmail.com>
> >> wrote:
> >> > Hi,
> >> >
> >> > That is wrong... if you do this, throws a NullPointerException...
> >> >
> >> >
> >> > 2008/2/1 Joey <jo...@gmail.com>
> >> >
> >> >> I found it . like this.
> >> >>
> >> >>        Map param = new HashMap();
> >> >>        ActionContext.getContext().setSession(param);
> >> >>
> >> >> Thanks for your help
> >> >>
> >> >> On Feb 1, 2008 4:53 PM, Joey <jo...@gmail.com> wrote:
> >> >> > Thanks,  I just checked testing class in Struts2 package,  but I am
> a
> >> >> > newbie for Struts2. I cannot understant Struts2 very well, so if
> you
> >> >> > don't mind, pls tell me which testing class I can learn from.
> >> >> >
> >> >> > Thanks again
> >> >> >
> >> >> >
> >> >> > On Feb 1, 2008 4:31 PM, Nils-Helge Garli Hegvik <ni...@gmail.com>
> >> >> wrote:
> >> >> > > You need to initialize the ActionContext in your unit tests. Take
> a
> >> >> > > look at the Struts 2 tests how it is solved there.
> >> >> > >
> >> >> > > Nils-H
> >> >> > >
> >> >> > >
> >> >> > > On Feb 1, 2008 9:28 AM, Joey Watson <jo...@gmail.com>
> >> >> wrote:
> >> >> > > > Hi everybody.
> >> >> > > >
> >> >> > > > I want to test a action class (Struts2) by JUnit. and then
> method
> >> in
> >> >> > > > this action need a value in session, then when I run my JUnit
> test
> >> >> > > > class,  ActionContext.getContext().getSession return null. (not
> >> >> > > > ActionContext.getContext().getSession().get("XXX") return
> null).
> >> >> > > > this action class is working fine in browser.
> >> >> > > >
> >> >> > > >
> >> >> > > > thanks for any help
> >> >> > > >
> >> >> > > > Joey
> >> >> > > >
> >> >> > > >
> >> ---------------------------------------------------------------------
> >> >> > > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> >> > > > For additional commands, e-mail: user-help@struts.apache.org
> >> >> > > >
> >> >> > > >
> >> >> > >
> >> >> > >
> >> ---------------------------------------------------------------------
> >> >> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> >> > > For additional commands, e-mail: user-help@struts.apache.org
> >> >> > >
> >> >> > >
> >> >> >
> >> >>
> >> >> ---------------------------------------------------------------------
> >> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> >> For additional commands, e-mail: user-help@struts.apache.org
> >> >>
> >> >>
> >> >
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >>
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: ActionContext.getContext().getSession return null when I test Struts2 action by JUnit.

Posted by Nils-Helge Garli Hegvik <ni...@gmail.com>.
Well, if that was a question, then I didn't see it....

I would certainly prefer implementing SessionAware, if it's an action
you're testing. But if you really need to set up the ActionContext you
have to create one first and the "activate" it. If you look at the
xwork source code for 2.0.5 you can see that the ActionContext is
lazily created for you when getContext() is invoked, whereas in 2.1.2,
the context is no longer being created if it does not exist.

Nils-H

On Mon, Aug 3, 2009 at 1:06 PM, Juanjo Cuadrado<jj...@gmail.com> wrote:
> And... what is the problem??? I have the same problem with junit test and
> actions. No matter how long ago the comment was written. He might have found
> out already, but I not and he not has written the solution here.
>
> But I wan to say a thing... When you does
> "ActionContext.getContext().setSession(param)" with xwork-2.1.2, you get a
> NullPointerException, but if you do it with xwork-2.0.5, it works.
>
> If any know what is happening, I would be extremely grateful.
>
>
> 2009/8/3 Nils-Helge Garli Hegvik <ni...@gmail.com>
>
>> He might have found out already, considering this was posted 1,5 years
>> ago...
>>
>> Nils-H
>>
>> On Mon, Aug 3, 2009 at 12:44 PM, Juanjo Cuadrado<jj...@gmail.com>
>> wrote:
>> > Hi,
>> >
>> > That is wrong... if you do this, throws a NullPointerException...
>> >
>> >
>> > 2008/2/1 Joey <jo...@gmail.com>
>> >
>> >> I found it . like this.
>> >>
>> >>        Map param = new HashMap();
>> >>        ActionContext.getContext().setSession(param);
>> >>
>> >> Thanks for your help
>> >>
>> >> On Feb 1, 2008 4:53 PM, Joey <jo...@gmail.com> wrote:
>> >> > Thanks,  I just checked testing class in Struts2 package,  but I am a
>> >> > newbie for Struts2. I cannot understant Struts2 very well, so if you
>> >> > don't mind, pls tell me which testing class I can learn from.
>> >> >
>> >> > Thanks again
>> >> >
>> >> >
>> >> > On Feb 1, 2008 4:31 PM, Nils-Helge Garli Hegvik <ni...@gmail.com>
>> >> wrote:
>> >> > > You need to initialize the ActionContext in your unit tests. Take a
>> >> > > look at the Struts 2 tests how it is solved there.
>> >> > >
>> >> > > Nils-H
>> >> > >
>> >> > >
>> >> > > On Feb 1, 2008 9:28 AM, Joey Watson <jo...@gmail.com>
>> >> wrote:
>> >> > > > Hi everybody.
>> >> > > >
>> >> > > > I want to test a action class (Struts2) by JUnit. and then method
>> in
>> >> > > > this action need a value in session, then when I run my JUnit test
>> >> > > > class,  ActionContext.getContext().getSession return null. (not
>> >> > > > ActionContext.getContext().getSession().get("XXX") return null).
>> >> > > > this action class is working fine in browser.
>> >> > > >
>> >> > > >
>> >> > > > thanks for any help
>> >> > > >
>> >> > > > Joey
>> >> > > >
>> >> > > >
>> ---------------------------------------------------------------------
>> >> > > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> > > > For additional commands, e-mail: user-help@struts.apache.org
>> >> > > >
>> >> > > >
>> >> > >
>> >> > >
>> ---------------------------------------------------------------------
>> >> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> > > For additional commands, e-mail: user-help@struts.apache.org
>> >> > >
>> >> > >
>> >> >
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> >> For additional commands, e-mail: user-help@struts.apache.org
>> >>
>> >>
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>

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


Re: ActionContext.getContext().getSession return null when I test Struts2 action by JUnit.

Posted by Juanjo Cuadrado <jj...@gmail.com>.
And... what is the problem??? I have the same problem with junit test and
actions. No matter how long ago the comment was written. He might have found
out already, but I not and he not has written the solution here.

But I wan to say a thing... When you does
"ActionContext.getContext().setSession(param)" with xwork-2.1.2, you get a
NullPointerException, but if you do it with xwork-2.0.5, it works.

If any know what is happening, I would be extremely grateful.


2009/8/3 Nils-Helge Garli Hegvik <ni...@gmail.com>

> He might have found out already, considering this was posted 1,5 years
> ago...
>
> Nils-H
>
> On Mon, Aug 3, 2009 at 12:44 PM, Juanjo Cuadrado<jj...@gmail.com>
> wrote:
> > Hi,
> >
> > That is wrong... if you do this, throws a NullPointerException...
> >
> >
> > 2008/2/1 Joey <jo...@gmail.com>
> >
> >> I found it . like this.
> >>
> >>        Map param = new HashMap();
> >>        ActionContext.getContext().setSession(param);
> >>
> >> Thanks for your help
> >>
> >> On Feb 1, 2008 4:53 PM, Joey <jo...@gmail.com> wrote:
> >> > Thanks,  I just checked testing class in Struts2 package,  but I am a
> >> > newbie for Struts2. I cannot understant Struts2 very well, so if you
> >> > don't mind, pls tell me which testing class I can learn from.
> >> >
> >> > Thanks again
> >> >
> >> >
> >> > On Feb 1, 2008 4:31 PM, Nils-Helge Garli Hegvik <ni...@gmail.com>
> >> wrote:
> >> > > You need to initialize the ActionContext in your unit tests. Take a
> >> > > look at the Struts 2 tests how it is solved there.
> >> > >
> >> > > Nils-H
> >> > >
> >> > >
> >> > > On Feb 1, 2008 9:28 AM, Joey Watson <jo...@gmail.com>
> >> wrote:
> >> > > > Hi everybody.
> >> > > >
> >> > > > I want to test a action class (Struts2) by JUnit. and then method
> in
> >> > > > this action need a value in session, then when I run my JUnit test
> >> > > > class,  ActionContext.getContext().getSession return null. (not
> >> > > > ActionContext.getContext().getSession().get("XXX") return null).
> >> > > > this action class is working fine in browser.
> >> > > >
> >> > > >
> >> > > > thanks for any help
> >> > > >
> >> > > > Joey
> >> > > >
> >> > > >
> ---------------------------------------------------------------------
> >> > > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> > > > For additional commands, e-mail: user-help@struts.apache.org
> >> > > >
> >> > > >
> >> > >
> >> > >
> ---------------------------------------------------------------------
> >> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> > > For additional commands, e-mail: user-help@struts.apache.org
> >> > >
> >> > >
> >> >
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >>
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: ActionContext.getContext().getSession return null when I test Struts2 action by JUnit.

Posted by Nils-Helge Garli Hegvik <ni...@gmail.com>.
He might have found out already, considering this was posted 1,5 years ago...

Nils-H

On Mon, Aug 3, 2009 at 12:44 PM, Juanjo Cuadrado<jj...@gmail.com> wrote:
> Hi,
>
> That is wrong... if you do this, throws a NullPointerException...
>
>
> 2008/2/1 Joey <jo...@gmail.com>
>
>> I found it . like this.
>>
>>        Map param = new HashMap();
>>        ActionContext.getContext().setSession(param);
>>
>> Thanks for your help
>>
>> On Feb 1, 2008 4:53 PM, Joey <jo...@gmail.com> wrote:
>> > Thanks,  I just checked testing class in Struts2 package,  but I am a
>> > newbie for Struts2. I cannot understant Struts2 very well, so if you
>> > don't mind, pls tell me which testing class I can learn from.
>> >
>> > Thanks again
>> >
>> >
>> > On Feb 1, 2008 4:31 PM, Nils-Helge Garli Hegvik <ni...@gmail.com>
>> wrote:
>> > > You need to initialize the ActionContext in your unit tests. Take a
>> > > look at the Struts 2 tests how it is solved there.
>> > >
>> > > Nils-H
>> > >
>> > >
>> > > On Feb 1, 2008 9:28 AM, Joey Watson <jo...@gmail.com>
>> wrote:
>> > > > Hi everybody.
>> > > >
>> > > > I want to test a action class (Struts2) by JUnit. and then method in
>> > > > this action need a value in session, then when I run my JUnit test
>> > > > class,  ActionContext.getContext().getSession return null. (not
>> > > > ActionContext.getContext().getSession().get("XXX") return null).
>> > > > this action class is working fine in browser.
>> > > >
>> > > >
>> > > > thanks for any help
>> > > >
>> > > > Joey
>> > > >
>> > > > ---------------------------------------------------------------------
>> > > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> > > > For additional commands, e-mail: user-help@struts.apache.org
>> > > >
>> > > >
>> > >
>> > > ---------------------------------------------------------------------
>> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> > > For additional commands, e-mail: user-help@struts.apache.org
>> > >
>> > >
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>

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


Re: ActionContext.getContext().getSession return null when I test Struts2 action by JUnit.

Posted by Juanjo Cuadrado <jj...@gmail.com>.
Hi,

That is wrong... if you do this, throws a NullPointerException...


2008/2/1 Joey <jo...@gmail.com>

> I found it . like this.
>
>        Map param = new HashMap();
>        ActionContext.getContext().setSession(param);
>
> Thanks for your help
>
> On Feb 1, 2008 4:53 PM, Joey <jo...@gmail.com> wrote:
> > Thanks,  I just checked testing class in Struts2 package,  but I am a
> > newbie for Struts2. I cannot understant Struts2 very well, so if you
> > don't mind, pls tell me which testing class I can learn from.
> >
> > Thanks again
> >
> >
> > On Feb 1, 2008 4:31 PM, Nils-Helge Garli Hegvik <ni...@gmail.com>
> wrote:
> > > You need to initialize the ActionContext in your unit tests. Take a
> > > look at the Struts 2 tests how it is solved there.
> > >
> > > Nils-H
> > >
> > >
> > > On Feb 1, 2008 9:28 AM, Joey Watson <jo...@gmail.com>
> wrote:
> > > > Hi everybody.
> > > >
> > > > I want to test a action class (Struts2) by JUnit. and then method in
> > > > this action need a value in session, then when I run my JUnit test
> > > > class,  ActionContext.getContext().getSession return null. (not
> > > > ActionContext.getContext().getSession().get("XXX") return null).
> > > > this action class is working fine in browser.
> > > >
> > > >
> > > > thanks for any help
> > > >
> > > > Joey
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > > For additional commands, e-mail: user-help@struts.apache.org
> > > >
> > > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail: user-help@struts.apache.org
> > >
> > >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: ActionContext.getContext().getSession return null when I test Struts2 action by JUnit.

Posted by Joey <jo...@gmail.com>.
I found it . like this.

        Map param = new HashMap();
        ActionContext.getContext().setSession(param);

Thanks for your help

On Feb 1, 2008 4:53 PM, Joey <jo...@gmail.com> wrote:
> Thanks,  I just checked testing class in Struts2 package,  but I am a
> newbie for Struts2. I cannot understant Struts2 very well, so if you
> don't mind, pls tell me which testing class I can learn from.
>
> Thanks again
>
>
> On Feb 1, 2008 4:31 PM, Nils-Helge Garli Hegvik <ni...@gmail.com> wrote:
> > You need to initialize the ActionContext in your unit tests. Take a
> > look at the Struts 2 tests how it is solved there.
> >
> > Nils-H
> >
> >
> > On Feb 1, 2008 9:28 AM, Joey Watson <jo...@gmail.com> wrote:
> > > Hi everybody.
> > >
> > > I want to test a action class (Struts2) by JUnit. and then method in
> > > this action need a value in session, then when I run my JUnit test
> > > class,  ActionContext.getContext().getSession return null. (not
> > > ActionContext.getContext().getSession().get("XXX") return null).
> > > this action class is working fine in browser.
> > >
> > >
> > > thanks for any help
> > >
> > > Joey
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail: user-help@struts.apache.org
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>

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


Re: ActionContext.getContext().getSession return null when I test Struts2 action by JUnit.

Posted by Joey <jo...@gmail.com>.
Thanks,  I just checked testing class in Struts2 package,  but I am a
newbie for Struts2. I cannot understant Struts2 very well, so if you
don't mind, pls tell me which testing class I can learn from.

Thanks again

On Feb 1, 2008 4:31 PM, Nils-Helge Garli Hegvik <ni...@gmail.com> wrote:
> You need to initialize the ActionContext in your unit tests. Take a
> look at the Struts 2 tests how it is solved there.
>
> Nils-H
>
>
> On Feb 1, 2008 9:28 AM, Joey Watson <jo...@gmail.com> wrote:
> > Hi everybody.
> >
> > I want to test a action class (Struts2) by JUnit. and then method in
> > this action need a value in session, then when I run my JUnit test
> > class,  ActionContext.getContext().getSession return null. (not
> > ActionContext.getContext().getSession().get("XXX") return null).
> > this action class is working fine in browser.
> >
> >
> > thanks for any help
> >
> > Joey
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

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


Re: ActionContext.getContext().getSession return null when I test Struts2 action by JUnit.

Posted by Nils-Helge Garli Hegvik <ni...@gmail.com>.
You need to initialize the ActionContext in your unit tests. Take a
look at the Struts 2 tests how it is solved there.

Nils-H

On Feb 1, 2008 9:28 AM, Joey Watson <jo...@gmail.com> wrote:
> Hi everybody.
>
> I want to test a action class (Struts2) by JUnit. and then method in
> this action need a value in session, then when I run my JUnit test
> class,  ActionContext.getContext().getSession return null. (not
> ActionContext.getContext().getSession().get("XXX") return null).
> this action class is working fine in browser.
>
>
> thanks for any help
>
> Joey
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

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