Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

java - Struts2.3.12 junit4 testcase request & session are null

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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...