s2sh4整合开发中使用JUnit做测试用例报错

J2EE 码拜 9年前 (2015-04-09) 1215次浏览 0个评论
 

这个是测试用例:

package com.gpy.oa.admin.identity.action;

import org.apache.struts2.StrutsJUnit4TestCase;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.opensymphony.xwork2.ActionProxy;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:D:/ws2014/oa/WebContent/WEB-INF/applicationContext*.xml"})
public class JobTestAction extends StrutsJUnit4TestCase<JobAction> {
	@Test
	public void selectJobs(){
		ActionProxy ap = this.getActionProxy("/admin/identity/selectJob.action");

		Assert.assertNotNull(ap);
	}
}

以JUnit运行时报错,说我在web.xml里面没有配置Spring控制器的<listener-class>没有配置,信息如下
SEVERE:   [45:21.782] ********** 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:   [45:21.809] Dispatcher initialization failed

web.xml中关于Spring的配置如下:

<!-- 配置Spring核心监听器 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>/WEB-INF/applicationContext*.xml</param-value>
  </context-param>
  
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

PS:我整个架构完成后,请求→Action→Service→DAO 这个流程是完全通畅的。所以对于测试用例这里报错,求教大神。

s2sh4整合开发中使用JUnit做测试用例报错
40分
<!-- 配置Spring核心监听器 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener> 

 <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext*.xml</param-value>
  </context-param>

调换一下位置

s2sh4整合开发中使用JUnit做测试用例报错
listener要配置在最前面
s2sh4整合开发中使用JUnit做测试用例报错
楼上的方法试试。
s2sh4整合开发中使用JUnit做测试用例报错
应该是顺序不对,还有注意一下路径,建议你放到src下面:
classpath:applicationContext*.xml
s2sh4整合开发中使用JUnit做测试用例报错
引用 2 楼 haorengoodman 的回复:
<!-- 配置Spring核心监听器 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener> 

 <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext*.xml</param-value>
  </context-param>

调换一下位置

我最开始就是先listener,再param的,一样的报错。

s2sh4整合开发中使用JUnit做测试用例报错
你测试的时候,没有去web.xml读取监听器,才会报呢样的错
s2sh4整合开发中使用JUnit做测试用例报错
引用 7 楼 u010669261 的回复:

你测试的时候,没有去web.xml读取监听器,才会报呢样的错

那该怎么写呢?第一次写测试用例,什么都不懂!

s2sh4整合开发中使用JUnit做测试用例报错
测试类继承StrutsSpringTestCase试试

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明s2sh4整合开发中使用JUnit做测试用例报错
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!