关闭

单元测试Struts2Spring项目的Action和Service

发表于:2013-11-13 11:11

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:zhao_perry    来源:51Testing软件测试网采编

  4.新建Service。
package action;
import org.springframework.stereotype.Service;
@Service
public class UserService {
/**
* 简单的返回用户名
*/
public String getName(String name) {
// 这个地方实际上应该调用Dao持久层的代码,在此简化了。
// 本文只是单元测试Struts2Spring整合中,Struts的Action和Spring注入的Service。
// Struts2Spring+Hibernate/Mybatis将在后续篇章中介绍。
return name;
}
}
  5.单元测试Action。
package unittest;
import org.apache.struts2.StrutsSpringTestCase;
import org.junit.Test;
import action.UserAction;
import com.opensymphony.xwork2.ActionProxy;
/**
* 测试StrutsSpring集成的时候,需要继承StrutsSpringTestCase这个类,
* 而单独测试Struts的时候,继承StrutsTestCase。
*
* @author http://blog.csdn.net/fansunion/
*
*/
public class ActionUnitTest extends StrutsSpringTestCase {
// 重写父类方法,指定配置文件的名字
protected String[] getContextLocations() {
return new String[] { "struts.xml", "applicationContext-spring.xml" };
}
@Test
public void testExecute() throws Exception {
ActionProxy proxy = getActionProxy("/unitTest");
UserAction test = (UserAction) proxy.getAction();
assertNotNull(test);
String result = proxy.execute();
assertEquals("success", result);
}
}
42/4<1234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号