當 struts2 加入 struts2-spring-plugin 的 JAR 檔後,執行 JUnit 或 Maven build 時發生 

SEVERE:   [50:59.081] ********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION **********
Looks like the Spring listener was not configured for your web app!
Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.
You might need to add the following to web.xml:
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
SEVERE:   [50:59.089] Dispatcher initialization failed

 

這時候 JUnit 程式的 extends 要由 StrutsTestCase 變為 StrutsSpringTestCase ,這樣才不會出錯。程式碼如下

package com.mis.example;

import com.opensymphony.xwork2.ActionSupport;

import org.apache.struts2.StrutsSpringTestCase;
import org.apache.struts2.StrutsTestCase;

//public class HelloWorldTest extends StrutsTestCase{
public class HelloWorldTest extends StrutsSpringTestCase {

    public void testHelloWorld() throws Exception {
        HelloWorld hello_world = new HelloWorld();
        String result = hello_world.execute();
        assertTrue("Expected a success result!",
                ActionSupport.SUCCESS.equals(result));
        assertTrue("Expected the default message!",
                hello_world.getText(HelloWorld.MESSAGE).equals(hello_world.getMessage()));
    }
}

 

參考:

http://blog.csdn.net/zj133520/article/details/12620763

http://kang36897.blog.163.com/blog/static/170473732010710101238126/

http://openhome.cc/Gossip/JUnit/

http://linuschien.blogspot.tw/2013/01/unit-test-to-struts2-action-with-spring.html

 

arrow
arrow
    文章標籤
    struts2-spring-plugin
    全站熱搜

    MIS 發表在 痞客邦 留言(0) 人氣()