StrutsConventionSpringJUnit4TestCase请求params null

时间:2014-10-14 12:04:11

标签: java spring struts2

我是S2的新手,尤其是单元测试动作类。我的问题是参数拦截器在单元测试期间没有为操作字段分配参数值。

问题的完整解释: 使用struts2-junit4-plugin 1.7.4我无法使用约定插件和带注释的类。我通过跟随this example找到了解决这个问题的方法,但如果我想使用inteceptors测试动作,即使我设置了MockHttpServletRequest的参数,我的动作字段也不会被params拦截器填充。

我赞美任何输入和建议如何解决这个问题,甚至更好地做动作测试!

依赖关系:

enter image description here

测试类: [注意 - 没有拦截器堆栈的第二次测试通过测试..]



        public class LoginActionTest extends StrutsConventionSpringJUnit4TestCase {

            /**
             * Invoke all interceptors and specify value of action class'
             * domain objects through request parameters.
             * @throws Exception Exception
             */
            @Test
            public void testInterceptorsBySettingRequestParameters() throws Exception {

                ActionProxy proxy = this.getActionProxy("/login");
                request.setParameter("email","test@user.com");
                request.setParameter("password","testuser");

                System.out.println("\n\nInside test class checking request parameters before execute call:\n\n" +
                        "email => " + request.getParameter("email") +
                        "\npassword => " + request.getParameter("password"));

    // EDIT: removed 
                //LoginAction loginAction = (LoginAction) proxy.getAction();

    // changed from
                //String result = loginAction.execute();
                // to

    String result = proxy.execute();

                System.out.println("\n\nInside test class checking request parameters after execute:\n\n" +
                        "email => " + request.getParameter("email") +
                        "\npassword => " + request.getParameter("password"));

                assertEquals("success", result);
            }

            /**
             * Skip interceptors and specify value of action class'
             * domain objects by setting them directly.
             * @throws Exception Exception
             */
            @Test
            public void testLoginActionWhitoutProxy() throws Exception {

                LoginAction testLA = new LoginAction();
                testLA.setEmail("test@user.com");
                testLA.setPassword("testuser");
                testLA.setSession(new HashMap()); // avoid NPE in execute
                String result = testLA.execute();

                assertEquals("Result is NOT what was expected [success] .. ", "success", result);

            }
        } // end class

控制台:



    13:06:36,683  INFO StrutsSpringObjectFactory:42 - ... initialized Struts-Spring integration successfully


    Inside test class checking request parameters before execute call:

    email => test@user.com
    password => testuser
    13:06:37,575  INFO ActionSupport:64 - Verifying login details..
    13:06:37,578  INFO ActionSupport:73 - Check user: 

            Login action ([package].bean.User@2e931d05) 
             with these fields: [firstName = test@user.com, lastName = eXuxIn/PXLQ7ou1/w2TaFzs8dtITa8IoMrxLG37oPatYsxPkJm/5CmPlmzPvjZZx]..

            test@user.com eXuxIn/PXLQ7ou1/w2TaFzs8dtITa8IoMrxLG37oPatYsxPkJm/5CmPlmzPvjZZx
    13:06:37,578  INFO ActionSupport:78 - Check request params beeing passed to the local settters: 

            null null






    13:06:37,774  INFO StrutsSpringObjectFactory:42 - ... initialized Struts-Spring integration successfully
    13:06:37,957  INFO ActionSupport:64 - Verifying login details..
    13:06:37,958  INFO ActionSupport:73 - Check user: 

            Login action ([package].bean.User@54d03269) 
             with these fields: [firstName = test@user.com, lastName = eXuxIn/PXLQ7ou1/w2TaFzs8dtITa8IoMrxLG37oPatYsxPkJm/5CmPlmzPvjZZx]..

            test@user.com eXuxIn/PXLQ7ou1/w2TaFzs8dtITa8IoMrxLG37oPatYsxPkJm/5CmPlmzPvjZZx
    13:06:37,958  INFO ActionSupport:78 - Check request params beeing passed to the local settters: 

            test@user.com testuser

堆栈跟踪:



    java.lang.NullPointerException
        at [package].action.LoginAction.execute(LoginAction.java:83)
        at [package].action.LoginActionTest.testInterceptorsBySettingRequestParameters(LoginActionTest.java:39)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

SOLUTION:

感谢到@Aleksandr_M指出我正确的方向。 我再次检查了S2 tutorial site,其中说明了:

  

...之后我可以调用actionProxy.execute()。这导致了Struts 2   框架要经历其正常的Servlet文件管理器和进程   actionProxy识别的动作的拦截器(在此   case是register.action)...

所以我删除了proxy.getAction()并执行了代理实例以强制S2完成正常的过程。

需要进行其他更改才能使其正常运行。这是测试代码:



        @Test
        public void testInterceptorsBySettingRequestParameters() throws Exception {

    // set request params for the invocationContext in super class
            request.addParameter("email","test@user.com");
            request.addParameter("password","testuser");

    // use proxy from super class to get actionProxy [don't create new instance]        
            proxy = getActionProxy("/login");

    // execute proxy in normal framework process        
            String result = proxy.execute();

    // check result     
            assertEquals("Result is NOT what was expected [success] .. ", "success", result);
        }