ts2+Spring的UnitTest编写(使用StrutsTestCase的子类StrutsSpringTestCase)
2012-02-06 23:37
316 查看
ts2+Spring的UnitTest编写(使用StrutsTestCase的子类StrutsSpringTestCase)
博客分类:StrutsSpring单元测试XMLWebStruts 我们都知道struts2有自己的对象工厂即obejectFactory,但是你也可以使用spring来作为对象工厂,继承了spring之后的单元测试要加什么东西呢?
答案是要加入下面的这些包:spring-web-2.5.6.jar,spring-context-2.5.6.jar,spring- beans-2.5.6.jar,struts2-spring-plugin-2.1.8.1.jar这四个包,在web.xml文件中还要加入如下的代码行:
Xml代码
![](http://kennylee26.iteye.com/images/icon_copy.gif)
![](http://kennylee26.iteye.com/images/icon_star.png)
![](http://kennylee26.iteye.com/images/spinner.gif)
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
如果你要编写测试类,要继承StrutsSpringTestCase,一定要这样做,我的测试类如下:
Java代码
![](http://kennylee26.iteye.com/images/icon_copy.gif)
![](http://kennylee26.iteye.com/images/icon_star.png)
![](http://kennylee26.iteye.com/images/spinner.gif)
import org.apache.struts2.StrutsSpringTestCase; import org.apache.struts2.dispatcher.mapper.ActionMapping; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionProxy; public class HelloWorldSpringTest extends StrutsSpringTestCase { // @Override // public String getContextLocations() { // return "edu/ku/it/si/tutorial/action/TestAccountAction-context.xml"; // } public void testGetActionMapping() throws Exception { ActionMapping mapping = getActionMapping("/begin/helloWorld.action"); assertNotNull(mapping); assertEquals("/begin", mapping.getNamespace()); assertEquals("helloWorld", mapping.getName()); } public void testGetActionProxy() throws Exception { request.setParameter("username", "FD"); ActionProxy proxy = getActionProxy("/begin/helloWorld.action"); assertNotNull(proxy); HelloWorld action = (HelloWorld) proxy.getAction(); assertNotNull(action); String result = proxy.execute(); assertEquals(Action.SUCCESS, result); assertEquals("FD", action.getUsername()); } }
import org.apache.struts2.StrutsSpringTestCase; import org.apache.struts2.dispatcher.mapper.ActionMapping; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionProxy; public class HelloWorldSpringTest extends StrutsSpringTestCase { // @Override // public String getContextLocations() { // return "edu/ku/it/si/tutorial/action/TestAccountAction-context.xml"; // } public void testGetActionMapping() throws Exception { ActionMapping mapping = getActionMapping("/begin/helloWorld.action"); assertNotNull(mapping); assertEquals("/begin", mapping.getNamespace()); assertEquals("helloWorld", mapping.getName()); } public void testGetActionProxy() throws Exception { request.setParameter("username", "FD"); ActionProxy proxy = getActionProxy("/begin/helloWorld.action"); assertNotNull(proxy); HelloWorld action = (HelloWorld) proxy.getAction(); assertNotNull(action); String result = proxy.execute(); assertEquals(Action.SUCCESS, result); assertEquals("FD", action.getUsername()); } }
测试类的内容和上一篇一样,只是继承类不一样,StrutsSpringTestCase是StrutsTestCase的子类,这个单元测试默认读取配置文件applicationContext.xml的位置是类路径的根目录,如果你把这个文件放在不同位置或者取了一个不同的名称可以通过覆盖父类中的protected java.lang.String getContextLocations()来指定你的配置文件。
其他的地方和之前那篇日志中的一样,但是之前写的那个没有和spring继承的单元测试就不能使用了,如果你运行会报下面的错误:
SEVERE: [19:33.501] ********** 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: [19:33.541] Dispatcher initialization failed
正如你所见到的我们已经指定了监听器,解决办法是吧你的测试类的父类改成StrutsSpringTestCase就可以解决问题。
原文完,详细见:http://kang36897.blog.163.com/blog/static/170473732010710101238126/
----------------------------------
后记:
网上在struts2+Spring编写单元测试的问题里,发现大多问题是spring的配置文件applicationContext.xml的路径或者多个时如何修改。其实很不明白为什么不在 applicationContext.xml主文件内倒入其他配置文件?这样web.xml不用配多个并且很清晰,而修改名字连applicationContext.xml都不存在的话我觉得是自己找事做罢了。
Xml代码
![](http://kennylee26.iteye.com/images/icon_copy.gif)
![](http://kennylee26.iteye.com/images/icon_star.png)
![](http://kennylee26.iteye.com/images/spinner.gif)
<import resource="classpath:spring-datasource.xml" /> <import resource="classpath:spring-dao.xml" /> <import resource="classpath:spring-business.xml" /> <import resource="classpath:spring-struts.xml" />
<import resource="classpath:spring-datasource.xml" /> <import resource="classpath:spring-dao.xml" /> <import resource="classpath:spring-business.xml" /> <import resource="classpath:spring-struts.xml" />
推荐使用这种方法。
相关文章推荐
- Struts2+Spring的UnitTest编写(使用StrutsTestCase的子类StrutsSpringTestCase)
- Struts2+Spring的UnitTest编写(使用StrutsTestCase的子类StrutsSpringTestCase)
- 使用简单的unittest编写接口测试脚本
- 最简单的unittest的TestCase子类的实例
- 最简单的unittest的TestCase子类的实例
- dbunit-spring-test和maven-dbunit-plugin使用
- StrutsSpringTestCase中使用convention报Couldn't get resource paths for class path resource的解决方案
- 使用unittest测试框架来写selenium的TestCase
- 最简单的unittest的TestCase子类的实例
- python unittest.TestLoader()类的几种寻找testcase的方法的使用
- 使用struts+spring+hibernate 组装web应用
- 使用struts+spring+hibernate 组装web应用
- 使用 Spring 更好地处理 Struts 动作
- 在Struts中使用 DispatchAction 及其子类
- Python unittest 使用基本过程
- Spring Test 整合 JUnit 4 使用总结
- Spring Boot的单元测试(Unit Test)
- 使用struts+spring+hibernate组装你的web应用架构
- Spring Test 整合 JUnit 4 使用
- (4.5.4)Android测试TestCase单元(Unit test)测试和instrumentationCase单元测试