集成测试Strtus Spring Hibernate

2008-02-23 10:12:39来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

Spring和Hibernate利用JUnit进行测试,大家已经很熟悉.

Struts则利用StrutsTest测试.

下面我们测试一个登录的Struts程序

LoginForm.Java 文件

package com.bcxy.struts.form;

import javax.Servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class LoginForm extends ActionForm {

private String user;

private String pass;

/**
* Returns the user.
* @return String
*/
public String getUser() {
return user;
}

/**
* Set the user.
* @param user The user to set
*/
public void setUser(String user) {
this.user = user;
}

/**
* Returns the pass.
* @return String
*/
public String getPass() {
return pass;
}

/**
* Set the pass.
* @param pass The pass to set
*/
public void setPass(String pass) {
this.pass = pass;
}

}

LoginAction.java 文件

package com.bcxy.struts.action;

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 com.bcxy.struts.form.LoginForm;

public class LoginAction extends Action {

/**
* Method execute
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
LoginForm loginForm = (LoginForm) form;
String user = loginForm.getUser();
String pass = loginForm.getPass();

if(user.equals(pass)){
return mapping.findForward("success");
}else{
return mapping.findForward("fail");
}
}

}

struts-config.XML 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>

<!-- ========== Data Source Configuration =============================== -->
<data-sources />

<!-- ========== Form Bean Definitions ================================== -->
<form-beans>
<form-bean name="loginForm" type="com.bcxy.struts.form.LoginForm">
<form-property name="user" type="java.lang.String" />
<form-property name="pass" type="java.lang.String" />
</form-bean>
</form-beans>

<!-- ========== Global Exception Definitions ============================== -->
<global-exceptions />

<!-- ========== Global Forward Definitions =============================== -->
<global-forwards />

<!-- ========== Action Mapping Definitions =============================== -->
<action-mappings>
<action
type="com.bcxy.struts.action.LoginAction"
name="loginForm"
path="/login"
scope="request"
validate="false">
<forward name="success" path="/success.JSP"/>
<forward name="fail" path="/fail.jsp"/>
</action>

</action-mappings>
<!-- ========== Controller Configuration ================================ -->
<controller />

<!-- ========== Message Resources Definitions ============================ -->

<!-- ========== Plug Ins Configuration ================================= -->
<message-resources parameter="com.bcxy.struts.ApplicationResources" />
</struts-config>

下面我们写一个测试文件,它继承strtustest的MockStrutsTestCase类.

/*
* Created on 2004-10-31
*
*/
package test;

import java.io.File;

import servletunit.struts.MockStrutsTestCase;

/**
* @author ezerg
*/
public class LoginTest extends MockStrutsTestCase {

public LoginTest() {
super();
}

/**
* @param arg0
*/
public LoginTest(String arg0) {
super(arg0);
}

public void setUp() throws Exception{
super.setUp();
//指明Web应用的根
File context = new File("defaultroot");
setContextDirectory(context);
}

public void tearDown() throws Exception{
super.tearDown();
}

public void testLogin(){
//设置action的path
setRequestPathInfo("/login.do");
//准备所需要的参数

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:在RIA大潮里湿了一下身--Flex

下一篇:Mini Java编译器(二)——语法(BNF)