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的经验的请指教!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值