You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Adam Hardy (struts)" <ah...@cyberspaceroad.com> on 2008/10/10 13:39:51 UTC

Re: any struts 2 unit testers out there?

I am unit testing my MVC layer and looking for the best way to override
ActionSupport.getText() cleanly and efficiently for testing purposes (from a
developer time point of view).

Rather than reinventing the wheel, is there anything out there now that
people are willing to share? (Did I miss it somewhere?)

Fortunately I already have my own Action subclass which inherits
ActionSupport, so it shouldn't be too difficult to implement, but it would
be nice to be able to sort out it quickly (in the background I hear the
unmistakeable splat of another unforeseen coding session hitting the project
time line...)

For anyone reading this msg in the archives, when I've got something
(self-created or other) I'll see if I can put it up on the web somewhere -
in the wiki or even in a msg here.

Regards
Adam



paulbrickell on 05/04/08 10:28, wrote:
>Yeah, I think for the moment I will simply override the getText method. The
other solutions do require
>much more effort.
>
>Actually I did not search Jira. Maybe there is already a request. I will
take a look.
>
>Thanks,
>
>Paul B.
>>
>>As Ian says, the mock objects supplied by XWork may make this all a lot 
>>easier, but in any case all this is predicated on the fact that you're 
>>using ActionSupport's implementation of TextProvider which doesn't make 
>>provision for mocking at test time. I think it would make a great deal 
>>of sense to improve that (open an enhancement request in Jira), but in 
>>the mean time, there's nothing preventing you from providing a getText() 
>>implementation that's more conducive to testing.
>>
>>I feel your pain on this one, and the right solution is to fix the 
>>framework (patches always welcome ;-), but it shouldn't be that hard to 
>>get around.
>>
>>L.
>>paulbrickell wrote:
>>> Actually thats an interesting post. To my mind it doesn't demonstrate a
>>> solution. It demonstrates the problem.
>>> 
>>> Here is why I think this.
>>> 
>>> Struts 2 is just frankly brilliant for people doing test first
>>> development.
>>> The classes you create really are POJOs. There are no dependencies on
>>> any
>>> framework in my action classes. For example parameters are parsed well
>>> away
>>> from my action and the values are set using simple properties, things
>>> like
>>> sessions are simply maps. It is a truly new world for those of who have
>>> suffered the horror of mocking http requests, responses, context and the
>>> like.
>>> 
>>> But this simple scenario brings all that pain flooding back. I add a
>>> line in
>>> my action like this...
>>> 
>>> String yadaYada = getText("some.doodad");
>>> 
>>> And my lovely world comes unraveled real quick. Now I need mock objects
>>> up
>>> the ying-yang.
>>> 
>>> I started using the tutorial from the link posted (I am an Arsenal fan
>>> btw,
>>> so got two for the price of one, thanks), but it still doesn't provide a
>>> simple solution to the problem. To get this to work I have to build not
>>> just
>>> the application context but a mass of supporting objects to get a web
>>> application framework up and running before I can test my simple POJO.
>>> 
>>> I am not going to give up just yet, but I think I am still going to have
>>> to
>>> look for another way.
>>> 
>>> 
>>> Relph,Brian wrote:
>>>>
>>>> I recommend creating an action context.  Here is the basic guide I
>>>> followed to do so:
>>>>
>>>> http://arsenalist.com/2007/06/18/unit-testing-struts-2-actions-spring-junit/ 
>>>>
>>>> If you are not using spring or the struts2 spring plugin, you can cut
>>>> out
>>>> all the code around the applicationContext.
>>>>
>>>>
>>>> -----Original Message-----
>>>> From: paulbrickell [mailto:paul.brickell@evolvedintelligence.com] 
>>>> Sent: Thursday, April 03, 2008 11:44 AM
>>>> To: user@struts.apache.org
>>>> Subject: Re: any struts 2 unit testers out there?
>>>>
>>>>
>>>> I am trying to deal with the same issue. Did you get any resolution?
>>>>
>>>> Following on from the reply asking for a stack trace, here is what I am
>>>> getting...
>>>>
>>>>
>>>> java.lang.NullPointerException
>>>> 	at
>>>> com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:299)
>>>> 	at
>>>> com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:172)
>>>> 	at
>>>> com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:87)
>>>> 	at
>>>> com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:80)
>>>>         <SNIP>
>>>> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>> 	at
>>>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>>> 	at
>>>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>>>> 	at java.lang.reflect.Method.invoke(Method.java:585)
>>>> 	at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
>>>> 	at
>>>> org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
>>>> 	at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
>>>> 	at
>>>> org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
>>>> 	at
>>>> org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
>>>> 	at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
>>>> 	at
>>>> org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
>>>> 	at
>>>> org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
>>>> 	at
>>>> org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
>>>> 	at
>>>> org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
>>>> 	at
>>>> org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
>>>> 	at
>>>> org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
>>>> 	at
>>>> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
>>>> 	at
>>>> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>>>> 	at
>>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
>>>> 	at
>>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
>>>> 	at
>>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
>>>> 	at
>>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
>>>>
>>>>
>>>> It is caused by ActionContext.getContext() returning null. Quite
>>>> obviously
>>>> I do not have an action context during my unit testing.
>>>>
>>>> Now I can certainly use the ActionContext.setContext() in my tests
>>>> setup
>>>> method to push one into thread local storage and that works OK. It
>>>> isn't
>>>> ideal though because ActionContext is a concrete class and so my
>>>> choices
>>>> then become a bit limited. 
>>>>
>>>> I could create an instance but when I try this I find I have to create
>>>> a
>>>> rather large object model to make it actually work. To the point where
>>>> I
>>>> despair and give up.
>>>>
>>>> Alternatively I could use a mock library (like easy mock). But I am not
>>>> inclined to include a mocking library that requires byte code rewriting
>>>> (not even for testing).
>>>>
>>>> What I really want to do is inject a text provider into the
>>>> ActionSupport
>>>> class. At the top of the ActionSupport class is this...
>>>>
>>>> private final transient TextProvider textProvider = new
>>>> TextProviderFactory().createInstance(getClass(), this);
>>>>
>>>> Damn its final and so I cannot inject my own text provider. 
>>>>
>>>> BUT it uses a factory, thats good. I know I will have a look at the
>>>> factory I bet I can monkey with that and inject a mock. Nope. It's all
>>>> instance based. No way I can get in there. And thats that. Now what do
>>>> I
>>>> do?
>>>>
>>>> I can see two (half) workable solutions.
>>>>
>>>> One is to override the the getText method in the action class when I
>>>> instantiate it during testing. So I end up doing this in all my action
>>>> unit tests...
>>>>
>>>>         Action action = new MyAction()
>>>>         {
>>>>             @Override
>>>>             public String getText(String textName)
>>>>             {
>>>>                 return "mocked";
>>>>             }
>>>>         };
>>>>
>>>> It works, but its cheese.
>>>>
>>>> Or two I can add a level of indirection in my action class, like so...
>>>>
>>>>
>>>>     String text =
>>>> MyTextProviderFactory.getInstance(class.name).getText("some.property");
>>>>
>>>> Then I can use a delegate to the real text provider during live code
>>>> and a
>>>> mock of my own text provider during testing. The question here is, Why
>>>> for
>>>> the love of Pete, why?
>>>>
>>>> So in conclusion there are at least four options for testing Action
>>>> classes that use get text.
>>>>
>>>> 1. Build an action context by hand. (Too hard) 2. Use a class rewriting
>>>> mocking library. (Not on my watch) 3. Mock the get text method.
>>>> (Cheese)
>>>> 4. Add another level of indirection. (Man thats just annoying)
>>>>
>>>> Comments?
>>>>
>>>> BTW If you got this far, thanks for taking the time.
>>>> Paul B.
>>>>
>>>>
>>>>
>>>>
>>>> Session Mwamufiya wrote:
>>>>> Hi All,
>>>>>
>>>>> Would someone let me know whether it's possible to tweak something so 
>>>>> that JUnit test code can run on an action method that calls the 
>>>>> ActionSupport method getText() to fetch string resources from a
>>>>> package.properties file.
>>>>> As it stands, I keep getting a null exception when getText() is called 
>>>>> during the unit test.
>>>>>
>>>>> Thanks,
>>>>> Session

-- 
View this message in context: http://www.nabble.com/any-struts-2-unit-testers-out-there--tp13437046p19916659.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


Re: any struts 2 unit testers out there?

Posted by "R. Duval" <rd...@ibest.com.br>.
As simples as it may seem. Assuming you're creating an instance on your setUp
method, and just to make things funnier, using a facade mock which is
expected to be action constructor parameter, it would look like this:

...
Mockery mockContext = new Mockery();
SomeFacade someFacadeMock;
MyAction myAction;

@Before
public void setUp() throws Exception {
    someFacadeMock = mockContext.mock(SomeFacade.class);
    myAction = new MyAction(someFacadeMock) {
        @Override
        public String getText(String textName) {
            return "dummy";
        }
    }
}
...

This way you can assume any calls to ActionSupport#getText(String) will
return dummy. You can even make some assertions on that (assuming you are
using it to set an action error), like:

...
assertTrue(myAction.getActionErrors.contains("dummy"));
...

By now you might have figured that, but worth the time to expose a possible
and simple solution.

Cya,


Adam Hardy (debian) wrote:
> 
> I am unit testing my MVC layer and looking for the best way to override
> ActionSupport.getText() cleanly and efficiently for testing purposes (from
> a developer time point of view).
> 
> Rather than reinventing the wheel, is there anything out there now that
> people are willing to share? (Did I miss it somewhere?)
> 
> Fortunately I already have my own Action subclass which inherits
> ActionSupport, so it shouldn't be too difficult to implement, but it would
> be nice to be able to sort out it quickly (in the background I hear the
> unmistakeable splat of another unforeseen coding session hitting the
> project time line...)
> 
> For anyone reading this msg in the archives, when I've got something
> (self-created or other) I'll see if I can put it up on the web somewhere -
> in the wiki or even in a msg here.
> 
> Regards
> Adam
> 

-- 
View this message in context: http://www.nabble.com/any-struts-2-unit-testers-out-there--tp13437046p24006147.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