Struts2.3.12 junit4 testcase request & session are null

≡放荡痞女 提交于 2019-12-18 07:05:05

问题


I have already gone through this solution : Struts 2 JUnit Plugin v2.2.3: Test Class Extending StrutsTestCase; 'request' is null

But I'm already not having struts2-spring plugin, and neither I'm using spring in my application.

I have 3 test-cases, 2 fails at request being null and one fails at session being null

First Stacktrace

java.lang.NullPointerException
    at org.apache.struts2.interceptor.MessageStoreInterceptor.before(MessageStoreInterceptor.java:228)
    at org.apache.struts2.interceptor.MessageStoreInterceptor.intercept(MessageStoreInterceptor.java:193)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    at actions.TestIoOptimizationAction.login(TestIoOptimizationAction.java:29)

corresponds to

            Collection actionErrors = (Collection) session.get(actionErrorsSessionKey);

Second & Third test stacktrace

java.lang.NullPointerException
    at org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
    at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
    at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    at org.apache.struts2.StrutsJUnit4TestCase.getActionProxy(StrutsJUnit4TestCase.java:149)

corresponds to

        String contextPath = ServletActionContext.getRequest().getContextPath();

I believe there is some dependency issues. I've tried removing a few dependencies one-by-one and in-combination, but neither worked.

Here's how my POM looks like :

<dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.paypal.sdk</groupId>
            <artifactId>rest-api-sdk</artifactId>
            <version>0.7.1</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
            <version>2.12</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils-core</artifactId>
            <version>1.8.3</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.5</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-servlet</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-jsp</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>${struts.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-tiles-plugin</artifactId>
            <version>${struts.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-junit-plugin</artifactId>
            <version>${struts.version}</version>
        </dependency>


        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>1.7.3</version>
        </dependency>

        <!--        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>-->

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-6.0</artifactId>
            <version>1.0.0.Final</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-api</artifactId>
            <version>2.1.11</version>
        </dependency>

        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>3.7.5</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>
        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>            
    </dependencies>
<properties>
        <struts.version>2.3.12</struts.version>
        <hibernate.version>3.6.10.Final</hibernate.version>
        <struts.jquery.version>3.5.1</struts.jquery.version>
        <netbeans.compile.on.save>all</netbeans.compile.on.save>
        <netbeans.hint.deploy.server>Tomcat</netbeans.hint.deploy.server>
    </properties>

Update - The tests looks like

@Test
public void login() throws Exception {
    request.setParameter("email", "nitin.cool4urchat@gmail.com");
    request.setParameter("password", "22");
    ActionProxy proxy = getActionProxy("login");
    String result = proxy.execute();
    assertEquals("success", result);
    System.out.println(response.getContentAsString());
}

@Test
public void testView() throws Exception {
    ActionProxy proxy = getActionProxy("/optimize/view");
    String result = proxy.execute();
    assertEquals("success", result);
    System.out.println(response.getContentAsString());

    proxy = getActionProxy("/optimize/view/23");
    result = proxy.execute();
    assertEquals("success", result);
    System.out.println(response.getContentAsString());

    proxy = getActionProxy("/optimize/view/110");
    result = proxy.execute();
    assertEquals("success", result);
    System.out.println(response.getContentAsString());
}

@Test
public void testSave() throws Exception {
    request.setContent("{\"id\":\"1\",\"name\":\"nitin\"}".getBytes());
    request.addHeader("Content-Type", "application/json");
    request.setMethod("POST");

    ActionProxy proxy = getActionProxy("/save");
    actions.IoOptimizationAction myAct = (actions.IoOptimizationAction) proxy.getAction();

    String result = proxy.execute();

    assertEquals("success", result);
    System.out.println(response.getContentAsString());
}

UPDATE I changed the test to executeAction as suggested & hence, the 2nd test now looks like

 @Test
    public void testView() throws Exception {
        System.out.println(ActionContext.getContext());
        System.out.println(executeAction("optimize/view"));
}

The error stacktrace is :

[2013-10-29 14:50:38.766] [ WARN] MockServletContext.getRealPath:298 - Couldn't determine real path of resource class path resource [org/apache/struts2/dispatcher/error_en_US.ftl]
java.io.FileNotFoundException: class path resource [org/apache/struts2/dispatcher/error_en_US.ftl] cannot be resolved to URL because it does not exist
    at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:179)
    at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:48)
    at org.springframework.mock.web.MockServletContext.getRealPath(MockServletContext.java:295)
    at freemarker.cache.WebappTemplateLoader.findTemplateSource(WebappTemplateLoader.java:129)
    at freemarker.cache.MultiTemplateLoader.findTemplateSource(MultiTemplateLoader.java:111)
    at freemarker.cache.TemplateCache.acquireTemplateSource(TemplateCache.java:646)
    at freemarker.cache.TemplateCache.findTemplateSource(TemplateCache.java:622)
    at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:346)
    at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:235)
    at freemarker.template.Configuration.getTemplate(Configuration.java:578)
    at freemarker.template.Configuration.getTemplate(Configuration.java:543)
    at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:841)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:569)
    at org.apache.struts2.StrutsJUnit4TestCase.executeAction(StrutsJUnit4TestCase.java:128)
    at actions.TestIoOptimizationAction.testView(TestIoOptimizationAction.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    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.runners.ParentRunner.run(ParentRunner.java:236)
    at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:520)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1060)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:911)
[2013-10-29 14:50:38.810] [ WARN] MockServletContext.getRealPath:298 - Couldn't determine real path of resource class path resource [org/apache/struts2/dispatcher/error_en.ftl]
java.io.FileNotFoundException: class path resource [org/apache/struts2/dispatcher/error_en.ftl] cannot be resolved to URL because it does not exist
    at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:179)
    at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:48)
    at org.springframework.mock.web.MockServletContext.getRealPath(MockServletContext.java:295)
    at freemarker.cache.WebappTemplateLoader.findTemplateSource(WebappTemplateLoader.java:129)
    at freemarker.cache.MultiTemplateLoader.findTemplateSource(MultiTemplateLoader.java:111)
    at freemarker.cache.TemplateCache.acquireTemplateSource(TemplateCache.java:646)
    at freemarker.cache.TemplateCache.findTemplateSource(TemplateCache.java:622)
    at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:346)
    at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:235)
    at freemarker.template.Configuration.getTemplate(Configuration.java:578)
    at freemarker.template.Configuration.getTemplate(Configuration.java:543)
    at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:841)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:569)
    at org.apache.struts2.StrutsJUnit4TestCase.executeAction(StrutsJUnit4TestCase.java:128)
    at actions.TestIoOptimizationAction.testView(TestIoOptimizationAction.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    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.runners.ParentRunner.run(ParentRunner.java:236)
    at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:520)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1060)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:911)
[2013-10-29 14:50:39.127] [ WARN] MockServletContext.getRealPath:298 - Couldn't determine real path of resource class path resource [org/apache/struts2/dispatcher/error.ftl]
java.io.FileNotFoundException: class path resource [org/apache/struts2/dispatcher/error.ftl] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/Users/hp/.m2/repository/org/apache/struts/struts2-core/2.3.12/struts2-core-2.3.12.jar!/org/apache/struts2/dispatcher/error.ftl
    at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:204)
    at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
    at org.springframework.mock.web.MockServletContext.getRealPath(MockServletContext.java:295)
    at freemarker.cache.WebappTemplateLoader.findTemplateSource(WebappTemplateLoader.java:129)
    at freemarker.cache.MultiTemplateLoader.findTemplateSource(MultiTemplateLoader.java:111)
    at freemarker.cache.TemplateCache.acquireTemplateSource(TemplateCache.java:646)
    at freemarker.cache.TemplateCache.findTemplateSource(TemplateCache.java:622)
    at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:346)
    at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:235)
    at freemarker.template.Configuration.getTemplate(Configuration.java:578)
    at freemarker.template.Configuration.getTemplate(Configuration.java:543)
    at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:841)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:569)
    at org.apache.struts2.StrutsJUnit4TestCase.executeAction(StrutsJUnit4TestCase.java:128)
    at actions.TestIoOptimizationAction.testView(TestIoOptimizationAction.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    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.runners.ParentRunner.run(ParentRunner.java:236)
    at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:520)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1060)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:911)
<html>
<head>
    <title>Struts Problem Report</title>
    <style>
        pre {
            margin: 0;
            padding: 0;
        }    
    </style>
</head>
<body>
    <h2>Struts Problem Report</h2>
    <p>
    Struts has detected an unhandled exception:
    </p>


<div id="exception-info">
<table>
    <tr>
        <td><strong>Messages</strong>:</td>
        <td>
        </td>
    </tr>
    <tr>
        <td><strong>File</strong>:</td>
        <td>org/apache/struts2/impl/StrutsActionProxy.java</td>
    </tr>
    <tr>
        <td><strong>Line number</strong>:</td>
        <td>69</td>
    </tr>

</table>
</div>


<div id="stacktraces">
<hr />
<h3>Stacktraces</h3>
<div class="stacktrace" style="padding-left: 0em">
    <strong>java.lang.NullPointerException</strong>
    <div>
    <pre>
    org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
    org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
    com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:536)
    org.apache.struts2.StrutsJUnit4TestCase.executeAction(StrutsJUnit4TestCase.java:128)
    actions.TestIoOptimizationAction.testView(TestIoOptimizationAction.java:44)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
    org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:520)
    org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1060)
    org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:911)
    </pre>
    </div>
</div>
</div>

<div class="footer">
<hr />
<p>
You are seeing this page because development mode is enabled.  Development mode, or devMode, enables extra
debugging behaviors and reports to assist developers.  To disable this mode, set:
<pre>
  struts.devMode=false
</pre>
in your <code>WEB-INF/classes/struts.properties</code> file.
</p>
</div>
</body>
</html>

回答1:


If you are using getActionProxy method to execute your actions then you need to set new session map into the invocation context.

ActionProxy actionProxy = getActionProxy("/action");
Map<String, Object> sessionMap = new HashMap<String, Object>();
actionProxy.getInvocation().getInvocationContext().setSession(sessionMap);
actionProxy.execute();

If you don't need reference to action proxy then you can use executeAction method. Note that executeAction will return actual output of the result, not the result returned from execute method.




回答2:


In my case I needed to call super.setUp from my test.




回答3:


To avoid request from being null:

request = new MockHttpServletRequest();

For session:

Map<String, Object> sessionMap = new HashMap<String, Object>();
actionProxy.getInvocation().getInvocationContext().setSession(sessionMap);


来源:https://stackoverflow.com/questions/19649952/struts2-3-12-junit4-testcase-request-session-are-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!