设为首页 加入收藏

TOP

如何使用Junit测试使用Spring框架的代码(二)
2014-11-24 12:02:24 来源: 作者: 【 】 浏览:96
Tags:如何 使用 Junit 测试 Spring 框架 代码
objects) {
return this.excuteAction("POST", url, port, objects);
}
/**
* Simulate Request to URL appoint by MockHttpServletRequest.
*
* @param method
* POST/GET
* @param url
* requestURL
* @param port
* int
* @param objects
* parameters
* @return ModelAndView
*/
public final ModelAndView excuteAction(final String method, final String url, final int port,
final Object[]... objects) {
MockHttpServletRequest request = new MockHttpServletRequest(method, url);
MockHttpServletResponse response = new MockHttpServletResponse();
request.setServerPort(port);
request.setLocalPort(port);
if (objects != null) {
for (Object[] object : objects) {
if (object != null && object.length == 2) {
request.addParameter(object[0].toString(), object[1].toString());
}
}
}
MockHttpSession session = new MockHttpSession();
request.setSession(session);
try {
return this.excuteAction(request, response);
} catch (Exception e) {
e.printStackTrace();
InfoLogUtil.error(e.toString());
}
return null;
}
}
测试类
[java]
public class LocationInfoTest extends JUnitActionBase {
/**
* TODO: write description for this method.
*/
@Test
public void testKeepAlive() {
Map paramMap = new HashMap();
paramMap.put("ACTION", "KEEP_ALIVE");
this.excuteAction("/IBEFacade", 8080, paramMap);
}
/**
* TODO: write description for this method.
*/
@Test
public void testLocationInfo() {
this.excuteAction("/IBEFacade", 8080, new Object[]{"ACTION", "LOCATION_INFO"});
}
}
另外还可以配合easymock/powermock框架来测试
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇利用JDBC连接取数据并导入到EXCEL.. 下一篇 Java乔晓松-ajax的概述和XMLHTTP..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: