-----Original Message-----
From: paulbrickell [mailto:[EMAIL PROTECTED]
Sent: Friday, April 04, 2008 3:04 AM
To: user@struts.apache.org
Subject: RE: any struts 2 unit testers out there?
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
(partial) 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.
Cheers,
Paul B.
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:[EMAIL PROTECTED]
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(Locali
zedTextUtil.java:299)
at
com.opensymphony.xwork2.TextProviderSupport.getText(TextProvid
erSupport.java:172)
at
com.opensymphony.xwork2.TextProviderSupport.getText(TextProvid
erSupport.java:87)
at
com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:80)
<SNIP>
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccess
orImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMeth
odAccessorImpl.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(MethodRo
adie.java:98)
at
org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThen
Afters(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(JUnit4
ClassRunner.java:51)
at
org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4Class
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoa
die.java:27)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadi
e.java:37)
at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRu
nner.java:42)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
(JUnit4TestReference.java:38)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestEx
ecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTest
s(RemoteTestRunner.java:460)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTest
s(RemoteTestRunner.java:673)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(Rem
oteTestRunner.java:386)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTest
Runner.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
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
View this message in context:
http://www.nabble.com/any-struts-2-unit-testers-out-there--tp13437046p
16467812.html Sent from the Struts - User mailing list archive at
Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
----------------------------------------------------------------------
CONFIDENTIALITY NOTICE This message and any included
attachments are
from Cerner Corporation and are intended only for the
addressee. The
information contained in this message is confidential and may
constitute inside or non-public information under international,
federal, or state securities laws. Unauthorized forwarding,
printing,
copying, distribution, or use of such information is strictly
prohibited and may be unlawful. If you are not the
addressee, please
promptly delete this message and notify the sender of the delivery
error by e-mail or you may call Cerner's corporate offices
in Kansas City, Missouri, U.S.A at (+1) (816)221-1024.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
View this message in context:
http://www.nabble.com/any-struts-2-unit-testers-out-there--tp1