You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jake Vang <va...@googlemail.com> on 2011/05/13 20:58:57 UTC

unit testing with junit + spring + struts2, NullPointerException with HttpServletRequest

hi,

i am using struts v2.1.8.1 + spring 3.0 + junit 4.0. it should be
noted that i am using convention plugin for spring.

my action class extends ActionSupport and implements
ServletRequestAware and ServletResponseAware. the method i am testing
inside my action class never returns a string literal (i.e. succes,
error, input, etc...), and always returns null. the reason is that i
am not forwarding users to any JSP page, but instead, i am writing
JSON results back out to the response stream.

i need to test my action class, and i have a test class that extends
StrutsSpringTestCase. i followed the example at
http://struts.apache.org/2.1.8.1/docs/struts-2-junit-plugin-tutorial.html,
which was very helpful. however, when my code writes JSON back out
using ServletOutputStream, i get a NullPointerException. The
NullPointerException happens because the HttpServletResponse is null.
(i.e. null pointer happens on ServletOutputStream os =
getHttpServletResponse().getOutputStream()).

is there a step i am missing here that explains why the
HttpServletResponse object/field is null?

is there anyway to test action classes that do not return the usual
string literals but the contents of the output stream?

thank you in advance.

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


Re: unit testing with junit + spring + struts2, NullPointerException with HttpServletRequest

Posted by vbalrog <vb...@free.fr>.
Hello,
Did you manage to solve the problem:
"There is no Action mapped for namespace /myPackage and action name
myAction. 
I have the same one, actions mapped with convention annotations are not
found when I try to test it with junit. I have actions mapped into
struts.xml and these ones work perfectly with junit. With the debugging mode
I found that not any of my actions mapped with annotations are found by the
xwork DefaultConfiguration and ConventionUnknownHandler classes.

--
View this message in context: http://struts.1045723.n5.nabble.com/unit-testing-with-junit-spring-struts2-NullPointerException-with-HttpServletRequest-tp4394036p4961170.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: unit testing with junit + spring + struts2, NullPointerException with HttpServletRequest

Posted by jaredtims <ja...@yahoo.com>.
In the link you referenced me to, i'm not sure i understand how to accomplish
"you can modify the configuration value "/WEB-INF/jsp" to the existing class
path,such as "com.foo.bar" to make the convention resolve class path
smoothly"?

I tried setting struts.convention.result.path=com.foo.bar, but it did not
work.  

I then tried struts.convention.result.path=/ and it removed the errors, but
now it does not resolve the actions.   I get the following error now:

"There is no Action mapped for namespace /myPackage and action name
myAction.

thanks

--
View this message in context: http://struts.1045723.n5.nabble.com/unit-testing-with-junit-spring-struts2-NullPointerException-with-HttpServletRequest-tp4394036p4913525.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: unit testing with junit + spring + struts2, NullPointerException with HttpServletRequest

Posted by "dan.zheng" <cm...@gmail.com>.
I think the action will not affect the unit test process,it only part of the
convention classloader getresource,the workaround is
http://stackoverflow.com/questions/7734695/couldnt-get-resource-paths-for-class-path-resource

hope it will be effective

On Sat, Oct 15, 2011 at 3:13 AM, jaredtims <ja...@yahoo.com> wrote:

> So will the Struts2 jUnit Plugin work with the Struts2 Convention Plugin?
>  I
> keep getting errrors such as this:
>
> 2011-10-14 14:15:42,380 WARN
> org.springframework.mock.web.MockServletContext.getResourcePaths:212 -
> Couldn't get resource paths for class path resource [WEB-INF/content/]
> java.io.FileNotFoundException: class path resource [WEB-INF/content/]
> cannot
> be resolved to URL because it does not exist
>
> All our code uses the convention annotations, so I'd rather not have to
> create xml action mappings just so i can run tests against my actions.
> Anybody know any work arounds?
>
> thanks
>
> --
> View this message in context:
> http://struts.1045723.n5.nabble.com/unit-testing-with-junit-spring-struts2-NullPointerException-with-HttpServletRequest-tp4394036p4903696.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: unit testing with junit + spring + struts2, NullPointerException with HttpServletRequest

Posted by jaredtims <ja...@yahoo.com>.
So will the Struts2 jUnit Plugin work with the Struts2 Convention Plugin?  I
keep getting errrors such as this:

2011-10-14 14:15:42,380 WARN 
org.springframework.mock.web.MockServletContext.getResourcePaths:212 -
Couldn't get resource paths for class path resource [WEB-INF/content/]
java.io.FileNotFoundException: class path resource [WEB-INF/content/] cannot
be resolved to URL because it does not exist

All our code uses the convention annotations, so I'd rather not have to
create xml action mappings just so i can run tests against my actions. 
Anybody know any work arounds?

thanks

--
View this message in context: http://struts.1045723.n5.nabble.com/unit-testing-with-junit-spring-struts2-NullPointerException-with-HttpServletRequest-tp4394036p4903696.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: unit testing with junit + spring + struts2, NullPointerException with HttpServletRequest

Posted by Jake Vang <va...@googlemail.com>.
never mind, i tinkered a little bit and read the API docs. if anyone
is interested, here's what i did.

ActionProxy proxy = getActionProxy("/dummy-action");
MyAction action = (MyAction)proxy.getAction();

MockHttpServletResponse response = new MockHttpServletResponse();
action.setServletResponse(response);

String result = action.dummyAction();
Assert.assertNull(result); //should always be null

String json = response.getContentAsString();
Assert.assertNotNull(json); //shouldn't be null

On Fri, May 13, 2011 at 2:58 PM, Jake Vang <va...@googlemail.com> wrote:
> hi,
>
> i am using struts v2.1.8.1 + spring 3.0 + junit 4.0. it should be
> noted that i am using convention plugin for spring.
>
> my action class extends ActionSupport and implements
> ServletRequestAware and ServletResponseAware. the method i am testing
> inside my action class never returns a string literal (i.e. succes,
> error, input, etc...), and always returns null. the reason is that i
> am not forwarding users to any JSP page, but instead, i am writing
> JSON results back out to the response stream.
>
> i need to test my action class, and i have a test class that extends
> StrutsSpringTestCase. i followed the example at
> http://struts.apache.org/2.1.8.1/docs/struts-2-junit-plugin-tutorial.html,
> which was very helpful. however, when my code writes JSON back out
> using ServletOutputStream, i get a NullPointerException. The
> NullPointerException happens because the HttpServletResponse is null.
> (i.e. null pointer happens on ServletOutputStream os =
> getHttpServletResponse().getOutputStream()).
>
> is there a step i am missing here that explains why the
> HttpServletResponse object/field is null?
>
> is there anyway to test action classes that do not return the usual
> string literals but the contents of the output stream?
>
> thank you in advance.
>

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