SSH测试之Action测试

    这段时间面试,当我说道我没有使用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秒,比启动服务器快了不知道多少倍了吧。。。。

你可能感兴趣的:(java,apache,struts,面试,ssh)