这段时间面试,当我说道我没有使用SSH做过项目时,面试官的表情基本都比较异常~~~~所以决定抓紧时间看看SSH的整合,忽悠忽悠面试官去。。。。
研究了一段时间的测试,发现越来越依赖测试了,当看到green bar的时候很爽啊。。。。
不废话了,没在网上发现具体讲述SSH测试的文章,记录下我的摸索。例子是使用的Spirng2.0技术手册上的在线书签的例子。
这里只测试Struts的Action。
RegisterForm.java
package org.bookmark.form;
import org.apache.struts.action.ActionForm;
public class RegisterForm extends ActionForm {
private String email;
private String username;
private String passwd;
private String passwd2;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getPasswd2() {
return passwd2;
}
public void setPasswd2(String passwd2) {
this.passwd2 = passwd2;
}
}
RegisterController.java
package org.bookmark.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.bookmark.form.RegisterForm;
public class RegisterController extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
RegisterForm f = (RegisterForm) form;
System.out.println("Hello" + f.getUsername());
return mapping.findForward("success");
}
}
应该都没什么可说的,Action里面就是打印下Form里面的用户名,并返回success的Forward。
看下配置文件。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/*.do</url-pattern>
</servlet-mapping>
</web-app>
需要解释吗?
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="registerForm" type="org.bookmark.form.RegisterForm"/>
</form-beans>
<action-mappings>
<action path="/register"
type="org.springframework.web.struts.DelegatingActionProxy"
name="registerForm"
scope="request"
input="/index.jsp"
>
<forward name="success" path="/index.jsp"/>
</action>
</action-mappings>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext-beans.xml"/>
</plug-in>
</struts-config>
也不需要解释吧?Struts和Spring的整合资料还是很多的。
application-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Middle tier application context definition for the image database.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean name="/register" class="org.bookmark.controller.RegisterController" />
</beans>
OK。可以写测试了。网上查了半天,好像一般测试Struts都使用StrutsTest框架。搜索一下就能找到。
RegisterControllerTest.java
package org.bookmark.controller;
import java.io.File;
import servletunit.struts.MockStrutsTestCase;
public class RegisterControllerTest extends MockStrutsTestCase {
public void testSuccessfulLogin() {
File f = new File("D:/JPrograms/框架/SSH/out/myweb");
setContextDirectory(f);
// setConfigFile("/WEB-INF/struts-config.xml");
setRequestPathInfo("/register.do");
addRequestParameter("username", "Ivan");
actionPerform();
verifyForward("success");
}
}
首先需要说明的是,StrutsTest框架是基于junit3.8的。不同点是,不是继承TestCase了,而是继承MockStrutsTestCase。然后编写方法测试。首先设置根路径,就是你要部署到服务器上的那个目录。第三行,如果提示说找不到Struts的配置文件,那么这行就起作用了。第四行是设置请求路径,就是前端form的action设置。可以加do,也可以不加。下一行是设置请求的参数,再下一行是执行请求。接着就是验证返回的是否是success的forward。
MockStrutsTestCase里面提供了一堆验证,具体参考它里面的文档。运行测试,green bar就出现了
耗时0.812秒,比启动服务器快了不知道多少倍了吧。。。。
分享到:
相关推荐
花了好多时间搞了这个单元测试,在网上找相关的资料竟然都没有,我只有自己花时间搞了,搞这个ssh2,action的单元测试,花了好长时间,所有要5分了。
这是一个集成struts2+hibernate+spring+ajax的小型测试项目!对于初次学习和很想复习这方面知识的朋友很有帮助!
里面有我做公司项目前用MyEclipse搭建ssh框架测试通过后的一个项目,此项目添加了aop和事务支持,有完整的action测试; 还有自己做的3到4页的MyEclipse搭建SSH的傻瓜式教程,还有oracle数据库连接引擎的配置的图片...
本资源是在“ssh框架搭建实例源码4”基础上进行较好的测试和修改;进行部分代码注释;成功移植到ssm(strut2、spring、mybatis)实现了“hibernate和mybatis的之CRUD封装比较”...
SSH整合开发的一个实例,项目中有两张表(学生表与班级表)它们的关系是一对多的关系,项目中的action包是应用的表示层(Struts2)dao包是应用的数据访问层(Hibernate)domain是对应的两个类,由对象来生成关系,...
struts2.0 Action不依赖于容器,允许Action脱离容器单独测试。如果需要,Struts2 Action仍然可以访问初始的Request和Response。 但是,其他的元素减少或者消除了直接访问HttpServletRequest和HttpServletResponse的...
Eclipse搭建一个SSH2的空白项目,配置好数据库的信息直接启动即可运行测试 http://localhost:8081/ssh2/three/exe.action
<action name="test" class="testAction"> <result>/test.jsp </action> 3,部署到Tomcat中并访问测试。 4,说明: 1,在写Action时要指定 @Controller 与 @Scope("prototype") 2,在struts.xml中...
声明式事务管理,include方式的模板管理,视图显示使用EL + struts标签库完成, 业务层 和 DAO 层层次清楚, 所有jar包已经提供,工程完整,可直接使用 完整的eclipse3.2.2 +Myeclipse 5.5 +tomcat6.0测试 JDK6.0...
ssh整合demo 整合, structs 附带了多个测试页面可以跳转,写了一个简单action,structs.xml 使用了模糊匹配, spring 注释方式 注入依赖,xml方式实现aop,hibernate 配置好数据库,可自动生成表,标准的model ,dao...
直接导入后 对DAO在spring注入sessionFactory,然后在Action中可以进行简单的调用,测试测试例子都在代码main函数写好了,希望对大家有帮助
经过测试: (Windows Server 2019,macOS Catalina和Ubuntu 20.04 / 18.04 / 16.04) /需要openssh-client软件包; apt install -y openssh-client /需要openssh-clients软件包; yum install -y openssh-...
struts2.1 spring2.5 hibernate3.2 很好的集成实例代码,Hibernate配置文件采用POjo注解的形势,spring管理action和BPO、DAO等 用MySQL数据库,用TestServer做JUnit测试时,会自动创建所需要的表
cn.itcast.elec.web.action:系统控制层,负责页面和项目后台的跳转 cn.itcast.elec.web.form:封装值对象(VO对象),对应页面传递的表单值的属性 junit:测试类 * 配置文件 放置到src的目录下: beans.xml:...
ssh的源代码加jar包测试使用action访问跳转到页面,业务层controller层 继承com.opensymphony.xwork2.ActionSupport
本资料详细介绍了SSH架构的优缺点,可作为面试资料备用! 常说的好处 开源 常说的坏处 配置文件过大我就不提了 struts 优点: 收集,验证表单数据相比传统servlet简单 优雅的实现可配置的请求转发 ...
免费获取Ubuntu SSH虚拟机;)这是什么该脚本可帮助您使用SSH获得虚拟Ubuntu服务器! 这适用于GitHub动作,感谢GitHub而不是我:... 注意:这仅用于测试和部署应用程序,我不鼓励滥用Github Action! 谢谢(c)Area69Lab
ssh-agent GitHub操作这个动作启动ssh-agent ... 在运行GitHub Action工作流以暂存项目,运行测试或构建映像时,您可能需要从私有存储库中获取其他库或供应商。 GitHub Actions只能访问其运行的存储库。 因此,为了访问
该GitHub Action使用GITHUB_WORKSPACE通过ssh通过rsync将GITHUB_WORKSPACE中的特定目录部署到服务器上的文件夹中。 此操作通常会遵循构建/测试操作,该操作会将可部署的代码GITHUB_WORKSPACE在GITHUB_WORKSPACE ,...
这个JSP源码是一个基于Struts2、Spring和Hibernate(SSH)框架,结合MySQL数据库的企业级新闻系统。整个系统采用MVC模式设计,实现了新闻的发布、编辑、删除、查询等功能,...系统经过多轮测试,确保在各种环境下稳定