struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Zepernick <Paul.Zepern...@healthsmart.com.INVALID>
Subject NPE when running unit test with struts rest plugin
Date Mon, 02 Nov 2020 13:48:28 GMT
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.

Mime
  • Unnamed multipart/related (inline, None, 0 bytes)
View raw message