struts2在耦合servlet api下测试Action的异常问题

环境:spring+struts2,使用了spring-struts2-plugin进行集成,正常启动服务器是ok的。但是我在做单元测试时发现了问。
我的struts2 Action中必须直接使用servlet request。代码如下:
String url =  "http://localhost:8080/dealer/list";
String classes = new HttpUtil(ServletActionContext.getRequest().post(url));


JUnit代码如下:
InvList action = new InvListAction();
assertEquals(ActionSupport.SUCCESS,action.execute());

问题出在这:我刚开始直接让testCase集成jnit的TestCase,debug时在Action类的new HttpUtil直接抛出了nullPoiter异常。想了想原因应该是测试类无法取得request对象。
后来用了struts2-junit-plugin,让testCase继承了StrutsTestCase。其他代码全部不变。这次debug时抛出了异常:
nothing will work until WebApplicationContextUtil returns a valid ApplicationContext.
you may need to add the following to web.xml:
<listen>
   <listened-class>org.springframework.web.context.ContextLoaderListened</listened-class>
</listener>

很奇怪,这个listener我是加在web.xml的,而且正常启动服务器都可以访问spring的bean的,证明配置是ok的。
有谁有使用过StrutsTestCase这个类测试request response的经验的请指教!

猜你喜欢

转载自bruce0508.iteye.com/blog/237620