struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lukasz Lenart <lukaszlen...@apache.org>
Subject Re: How to Mock getText("customerLabel") method in junit?
Date Thu, 29 Aug 2013 10:23:07 GMT
2013/8/29 Mohit Gupta <motgupta@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


Mime
View raw message