You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mohit Gupta <mo...@gmail.com> on 2013/08/29 11:13:37 UTC

How to Mock getText("customerLabel") method in junit?

I am facing one issue while writing the junit of my struts 2 action
class.My action class has following code
    snippet

    getText("customerLabel")

While execution of junit i get below error though works fine thru
application(i think
    Struts controller loads it while normal web flow which does not happen
during junit)

    java.lang.NullPointerException
at
com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:361)
at
com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:208)
at
com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:123)
at com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:103)

I could get it working with a workaround by below code sniipet in my test
class and asiign it to my my action annotated with  annotation TestedObject



      action = new Myction()
        {
          @Override
          public String getText(String textName)
          {
            return "mockedLabel";
          }
        };

 But i am looking for clean solution?

Re: How to Mock getText("customerLabel") method in junit?

Posted by Lukasz Lenart <lu...@apache.org>.
2013/8/29 Mohit Gupta <mo...@gmail.com>:
> I am facing one issue while writing the junit of my struts 2 action
> class.My action class has following code
>     snippet
>
>     getText("customerLabel")
>
> While execution of junit i get below error though works fine thru
> application(i think
>     Struts controller loads it while normal web flow which does not happen
> during junit)
>
>     java.lang.NullPointerException
> at
> com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:361)
> at
> com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:208)
> at
> com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:123)
> at com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:103)
>
> I could get it working with a workaround by below code sniipet in my test
> class and asiign it to my my action annotated with  annotation TestedObject
>
>
>
>       action = new Myction()
>         {
>           @Override
>           public String getText(String textName)
>           {
>             return "mockedLabel";
>           }
>         };
>
>  But i am looking for clean solution?

Mocking as your solution is perfectly fine. If you want you can do it
like that in setUp:

ValueStack valueStack =
container.getInstance(ValueStackFactory.class).createValueStack()
ActionContext.setContext(new ActionContext())
ActionContext.getContext().setValueStack(valueStack)

but it doesn't look more readable ;-)


Regards
-- 
Ɓukasz
+ 48 606 323 122 http://www.lenart.org.pl/

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


Re: How to Mock getText("customerLabel") method in junit?

Posted by "bphillips@ku.edu" <bp...@ku.edu>.
You may want to checkout this tutorial and the example code that goes with
it:

  http://struts.apache.org/release/2.3.x/docs/unit-testing.html 





--
View this message in context: http://struts.1045723.n5.nabble.com/How-to-Mock-getText-customerLabel-method-in-junit-tp5713562p5713597.html
Sent from the Struts - User mailing list archive at Nabble.com.

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