You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Paul Zepernick <Pa...@healthsmart.com.INVALID> on 2020/11/02 13:48:28 UTC
NPE when running unit test with struts rest plugin
Struts: 5.2.25
Plugins: struts2-rest, struts2-spring, struts2-convention
junit 4
The test below is currently failing in the RestWorkflowInterceptor 205
@Test
public void testValidation() throws Exception {
// the login uses the create() which expects the POST method
request.setMethod("POST");
ActionProxy proxy = getActionProxy("/security/login");
Assert.assertNotNull(proxy);
LoginAction action = (LoginAction)proxy.getAction();
Assert.assertNotNull(action);
// THIS IS THE LINE THAT TRIGGERS THE FAILURE
String result = proxy.execute();
Assert.assertTrue("expected some field errors", !action.getFieldErrors().isEmpty());
}
Here is the line in the RestWorkflowInterceptor 203 that is returning a null and creates the NPE condition a couple lines down
ActionMapping mapping = (ActionMapping) ActionContext.getContext().get(ServletActionContext.ACTION_MAPPING);
I have verified that there are items in the context, but the struts.actionMapping key is missing.
I am not sure if this is related, but I have to override getActionProxy() to overcome a different NPE issue
@Override
protected ActionProxy getActionProxy(String uri) {
// the super.getActionProxy() already sets this, but does it at the end of the method.
// the ActionProxy proxy = ... line is already expecting it to be present and produces a NPE
ServletActionContext.setServletContext(servletContext);
ServletActionContext.setRequest(request);
ServletActionContext.setResponse(response);
return super.getActionProxy(uri);
}
Is there something else that has to be mocked to correctly setup the ActionContext for the RestWorkflowInterceptor?
Below is the entire test class for reference. The IWSStrutsSpringTestCase is extending StrutsSpringJUnit4TestCase, is providing the override of the getActionProxy, and is being annotated with @WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = LoginActionSpringConfig.class)
public class LoginActionTest extends IwsStrutsSpringTestCase<LoginAction> {
@Test
public void getActionMapping() {
ActionMapping mapping = getActionMapping("/security/login");
Assert.assertNotNull(mapping);
Assert.assertEquals("/security", mapping.getNamespace());
Assert.assertEquals("login", mapping.getName());
}
@Test
public void testValidation() throws Exception {
// the login uses the create() which expects the POST method
request.setMethod("POST");
ActionProxy proxy = getActionProxy("/security/login");
Assert.assertNotNull(proxy);
LoginAction action = (LoginAction)proxy.getAction();
Assert.assertNotNull(action);
String result = proxy.execute();
Assert.assertTrue("expected some field errors", !action.getFieldErrors().isEmpty());
}
}
Paul R Zepernick | Sr Programmer Analyst
HealthSmart
paul.zepernick@healthsmart.com | www.HealthSmart.com<http://www.healthsmart.com/>
[HS email footer]<http://www.healthsmart.com/> [if_square-linkedin_317725 (3)] <http://www.linkedin.com/company/healthsmart> [if_square-twitter_317723 (3)] <http://twitter.com/healthsmart> [if_square-facebook_317727 (2)] <http://www.facebook.com/healthsmart>
Disclaimer: This communication and any files transmitted with it may contain information that is privileged, confidential and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is strictly prohibited. If you received this communication in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you.
Re: NPE when running unit test with struts rest plugin
Posted by Lukasz Lenart <lu...@apache.org>.
pon., 9 lis 2020 o 14:38 Paul Zepernick
<Pa...@healthsmart.com.invalid> napisał(a):
>
> Thank you very much, this has resolved the issue! I have created a ticket in Jira.
>
> https://issues.apache.org/jira/browse/WW-5095
Great, thanks a lot!
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: NPE when running unit test with struts rest plugin
Posted by Paul Zepernick <Pa...@healthsmart.com.INVALID>.
Thank you very much, this has resolved the issue! I have created a ticket in Jira.
https://issues.apache.org/jira/browse/WW-5095
Paul R Zepernick | Sr Programmer Analyst
HealthSmart
paul.zepernick@healthsmart.com | www.HealthSmart.com
-----Original Message-----
From: Lukasz Lenart <lu...@apache.org>
Sent: Saturday, November 7, 2020 8:51 AM
To: Struts Users Mailing List <us...@struts.apache.org>
Subject: Re: NPE when running unit test with struts rest plugin
NOTICE: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
śr., 4 lis 2020 o 17:29 Paul Zepernick
<Pa...@healthsmart.com.invalid> napisał(a):
> I have posted an example project that reproduces the error in the
> junit test
>
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgith
> ub.com%2Fzepernick%2Fstruts25-rest-junit&data=04%7C01%7CPaul.Zeper
> nick%40healthsmart.com%7C51b18c36edb9455e56dc08d8832422d3%7C2ce547c5e8
> 0a40628a56f25adceefb52%7C0%7C0%7C637403538520826158%7CUnknown%7CTWFpbG
> Zsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%
> 3D%7C1000&sdata=R67dd6goXC0LKd7SiGGclmYcU0rsUDn1i53CyzjLmVw%3D&
> ;reserved=0
I think I have found the problem, mapping is not set in getActionProxy(), you can fix it by overriding it like this:
protected ActionProxy getActionProxy(String uri) {
request.setRequestURI(uri);
ActionMapping mapping = getActionMapping(request);
String namespace = mapping.getNamespace();
String name = mapping.getName();
String method = mapping.getMethod();
Configuration config = configurationManager.getConfiguration();
ActionProxy proxy =
config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, new HashMap<String,
Object>(), true, false);
initActionContext(proxy.getInvocation().getInvocationContext());
// this is normally done in onSetUp(), but we are using Struts internal
// objects (proxy and action invocation)
// so we have to hack around so it works
ServletActionContext.setServletContext(servletContext);
ServletActionContext.setRequest(request);
ServletActionContext.setResponse(response);
ServletActionContext.getContext().put(ServletActionContext.ACTION_MAPPING,
mapping);
return proxy;
}
Fee free to open a JIRA ticket and as I'm going to prepare yet another 2.5.x release I can address this issue
Regards
--
Łukasz
+ 48 606 323 122
+ https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.l
+ enart.org.pl%2F&data=04%7C01%7CPaul.Zepernick%40healthsmart.com%7C
+ 51b18c36edb9455e56dc08d8832422d3%7C2ce547c5e80a40628a56f25adceefb52%7C
+ 0%7C0%7C637403538520826158%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDA
+ iLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=jWn%2
+ B4tFUL53LvuprGz8l6bNEMWrX8d1FMLqlR1m5tJI%3D&reserved=0
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Disclaimer: This communication and any files transmitted with it may contain information that is privileged, confidential and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is strictly prohibited. If you received this communication in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you.
Re: NPE when running unit test with struts rest plugin
Posted by Lukasz Lenart <lu...@apache.org>.
śr., 4 lis 2020 o 17:29 Paul Zepernick
<Pa...@healthsmart.com.invalid> napisał(a):
> I have posted an example project that reproduces the error in the junit test
>
> https://github.com/zepernick/struts25-rest-junit
I think I have found the problem, mapping is not set in
getActionProxy(), you can fix it by overriding it like this:
protected ActionProxy getActionProxy(String uri) {
request.setRequestURI(uri);
ActionMapping mapping = getActionMapping(request);
String namespace = mapping.getNamespace();
String name = mapping.getName();
String method = mapping.getMethod();
Configuration config = configurationManager.getConfiguration();
ActionProxy proxy =
config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, new HashMap<String,
Object>(), true, false);
initActionContext(proxy.getInvocation().getInvocationContext());
// this is normally done in onSetUp(), but we are using Struts internal
// objects (proxy and action invocation)
// so we have to hack around so it works
ServletActionContext.setServletContext(servletContext);
ServletActionContext.setRequest(request);
ServletActionContext.setResponse(response);
ServletActionContext.getContext().put(ServletActionContext.ACTION_MAPPING,
mapping);
return proxy;
}
Fee free to open a JIRA ticket and as I'm going to prepare yet another
2.5.x release I can address this issue
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: NPE when running unit test with struts rest plugin
Posted by Paul Zepernick <Pa...@healthsmart.com.INVALID>.
Lukasz,
I have posted an example project that reproduces the error in the junit test
https://github.com/zepernick/struts25-rest-junit
Paul R Zepernick | Sr Programmer Analyst
HealthSmart
paul.zepernick@healthsmart.com | www.HealthSmart.com
-----Original Message-----
From: Paul Zepernick <Pa...@healthsmart.com.INVALID>
Sent: Tuesday, November 3, 2020 2:42 PM
To: Struts Users Mailing List <us...@struts.apache.org>
Subject: RE: NPE when running unit test with struts rest plugin
NOTICE: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
Sure, I will prepare an example on Github and let you know when it is available
Paul R Zepernick | Sr Programmer Analyst HealthSmart
Office: 517 582 6525
paul.zepernick@healthsmart.com | https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.healthsmart.com%2F&data=04%7C01%7CPaul.Zepernick%40healthsmart.com%7C346f28586cf44ead92a708d880308637%7C2ce547c5e80a40628a56f25adceefb52%7C0%7C1%7C637400293195116068%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=6eei482P2I3rZ0Dv%2BjG2csKMbht701IYLAdKdIgT0fY%3D&reserved=0
-----Original Message-----
From: Lukasz Lenart <lu...@apache.org>
Sent: Tuesday, November 3, 2020 2:37 PM
To: Struts Users Mailing List <us...@struts.apache.org>
Subject: Re: NPE when running unit test with struts rest plugin
NOTICE: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
wt., 3 lis 2020 o 13:24 Paul Zepernick
<Pa...@healthsmart.com.invalid> napisał(a):
> Yes, I am using the struts2-junit plugin and extending StrutsSpringJUnit4TestCase from the plugin.
Would you mind preparing a small example and put it on Github?
Regards
--
Łukasz
+ 48 606 323 122
+ https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.l
+ enart.org.pl%2F&data=04%7C01%7CPaul.Zepernick%40healthsmart.com%7C
+ 346f28586cf44ead92a708d880308637%7C2ce547c5e80a40628a56f25adceefb52%7C
+ 0%7C1%7C637400293195116068%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDA
+ iLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=VFoI2
+ p4yApykSgTPZOGXCNGN6KzDfzQiga2hyA8E0sE%3D&reserved=0
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Disclaimer: This communication and any files transmitted with it may contain information that is privileged, confidential and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is strictly prohibited. If you received this communication in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
RE: NPE when running unit test with struts rest plugin
Posted by Paul Zepernick <Pa...@healthsmart.com.INVALID>.
Sure, I will prepare an example on Github and let you know when it is available
Paul R Zepernick | Sr Programmer Analyst
HealthSmart
Office: 517 582 6525
paul.zepernick@healthsmart.com | www.HealthSmart.com
-----Original Message-----
From: Lukasz Lenart <lu...@apache.org>
Sent: Tuesday, November 3, 2020 2:37 PM
To: Struts Users Mailing List <us...@struts.apache.org>
Subject: Re: NPE when running unit test with struts rest plugin
NOTICE: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
wt., 3 lis 2020 o 13:24 Paul Zepernick
<Pa...@healthsmart.com.invalid> napisał(a):
> Yes, I am using the struts2-junit plugin and extending StrutsSpringJUnit4TestCase from the plugin.
Would you mind preparing a small example and put it on Github?
Regards
--
Łukasz
+ 48 606 323 122 https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.lenart.org.pl%2F&data=04%7C01%7CPaul.Zepernick%40healthsmart.com%7C9f1102882705449c0cb708d8802fd114%7C2ce547c5e80a40628a56f25adceefb52%7C0%7C0%7C637400290150028866%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=Lx9EpUy6swkqB65Qf84Zy1uaWQHCY6%2FzM%2Fp0%2Bu%2F35U8%3D&reserved=0
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Disclaimer: This communication and any files transmitted with it may contain information that is privileged, confidential and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is strictly prohibited. If you received this communication in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: NPE when running unit test with struts rest plugin
Posted by Lukasz Lenart <lu...@apache.org>.
wt., 3 lis 2020 o 13:24 Paul Zepernick
<Pa...@healthsmart.com.invalid> napisał(a):
> Yes, I am using the struts2-junit plugin and extending StrutsSpringJUnit4TestCase from the plugin.
Would you mind preparing a small example and put it on Github?
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: NPE when running unit test with struts rest plugin
Posted by Paul Zepernick <Pa...@healthsmart.com.INVALID>.
Yes, I am using the struts2-junit plugin and extending StrutsSpringJUnit4TestCase from the plugin.
Paul R Zepernick | Sr Programmer Analyst
HealthSmart
Office: 517 582 6525
paul.zepernick@healthsmart.com | www.HealthSmart.com
-----Original Message-----
From: Lukasz Lenart <lu...@apache.org>
Sent: Tuesday, November 3, 2020 1:16 AM
To: Struts Users Mailing List <us...@struts.apache.org>
Subject: Re: NPE when running unit test with struts rest plugin
NOTICE: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
pon., 2 lis 2020 o 14:48 Paul Zepernick
<Pa...@healthsmart.com.invalid> napisał(a):
> Struts: 5.2.25
>
> Plugins: struts2-rest, struts2-spring, struts2-convention junit 4
>
>
Did you try to use struts2-junit plugin and based your tests
on StrutsJUnit4TestCase?
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstruts.apache.org%2Fplugins%2Fjunit%2F&data=04%7C01%7CPaul.Zepernick%40healthsmart.com%7Cea5207226d3940a0902a08d87fbfeefb%7C2ce547c5e80a40628a56f25adceefb52%7C0%7C0%7C637399809618813651%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=MsGQZtBlw459VufXJY4dA%2BMaa8jW%2BEmH3i5uoXDv8GQ%3D&reserved=0
Regards
--
Łukasz
+ 48 606 323 122 https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.lenart.org.pl%2F&data=04%7C01%7CPaul.Zepernick%40healthsmart.com%7Cea5207226d3940a0902a08d87fbfeefb%7C2ce547c5e80a40628a56f25adceefb52%7C0%7C0%7C637399809618813651%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=UHMX7w%2BVMV%2BYXpHEgiwIEYDD8X4hGSVlr2O%2FJMwWlYA%3D&reserved=0
Disclaimer: This communication and any files transmitted with it may contain information that is privileged, confidential and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is strictly prohibited. If you received this communication in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: NPE when running unit test with struts rest plugin
Posted by Lukasz Lenart <lu...@apache.org>.
pon., 2 lis 2020 o 14:48 Paul Zepernick
<Pa...@healthsmart.com.invalid> napisał(a):
> Struts: 5.2.25
>
> Plugins: struts2-rest, struts2-spring, struts2-convention
> junit 4
>
>
Did you try to use struts2-junit plugin and based your tests
on StrutsJUnit4TestCase?
https://struts.apache.org/plugins/junit/
Regards
--
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/