package com.gdnt.rnd.ework.servlet;
import org.apache.log4j.Logger;
import java.io.IOException; import java.io.File; import java.util.Enumeration;
import javax.servlet.ServletException; import javax.servlet.http.HttpSession;
import junit.textui.TestRunner;
import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import com.gdnt.rnd.config.TestPropertyManager; import com.kenoah.base.action.Action; import com.meterware.servletunit.ServletRunner; import com.meterware.servletunit.ServletUnitClient; import com.meterware.servletunit.InvocationContext; import com.meterware.httpunit.HttpUnitOptions; import com.meterware.httpunit.WebClient; import com.meterware.httpunit.WebRequest; import com.meterware.httpunit.WebResponse; import com.meterware.httpunit.PostMethodWebRequest;
import org.xml.sax.SAXException;
public class GoHomeTest extends TestCase //在编写一个单元测试类的时候要继承junit.framework.TestCase; { static { TestPropertyManager.init();//这个是加载一些配置文件,需要自己写的,方法代码附后面 }
private static final Logger logger = Logger.getLogger(GoHomeTest.class);//日志记录
public static void main(String[] args) { TestRunner.run(GoHomeTest.class);//这个就可以当作普通的工程来运行,不用特意去用JUNIT运行了 }
public static Test suite() { TestSuite suite = new TestSuite(GoHomeTest.class);//有这个才能作为Test,原因暂不明 return suite; }
protected void setUp() throws Exception { super.setUp(); }
protected void tearDown() throws Exception { super.tearDown(); }
public void testGoHomeOK()//JUNIT或HttpUnit如果需要运行,所测试方法的名称都要在前面加上test否则和缺少suite方法一样,说不是No Test in.. { HttpUnitOptions.setExceptionsThrownOnScriptError(false);//如果需要用到页面,可以屏蔽页面本身的Script错误. try { ServletRunner runner = new ServletRunner(new File( "web/WEB-INF/web.xml"), "/eform");//启动容易,指定使用的配置文件和URL路径 ServletUnitClient client = runner.newClient();//产生客户端 ClsUlity clsUlity = new ClsUlity();//自己写的类 WebResponse responseLogin = clsUlity.Login("webmaster", "", client); this.updateCookie(client, responseLogin);//这个部分是登录,因为后面要验证,这个工程是更新cookie. WebRequest requestGoHome = new PostMethodWebRequest( "http://localhost/eform/GoHome.action"); InvocationContext contextGoHome = client .newInvocation(requestGoHome); contextGoHome.pushFilter(contextGoHome.getRequest(), contextGoHome .getResponse()); HttpSession sessionGoHome = client.getSession(true);//获取所测试的Servlet的session对象 sessionGoHome.setAttribute("user", "pp");//在session里放东西,前面是key,后面是value Enumeration enumGoHomeSession = sessionGoHome.getAttributeNames();//拿出session里面所有的东西名称,就是key while(enumGoHomeSession.hasMoreElements()){ String name = (String)enumGoHomeSession.nextElement(); System.out.println(name); } Object usernameValue =sessionGoHome.getAttribute("user");//拿出指定的session中user名称的值 System.out.println(usernameValue);//打印值 ServletController servlet = (ServletController) contextGoHome .getServlet(); Action actionGoHome = servlet.getAction(contextGoHome.getRequest(), contextGoHome.getResponse()); try { String actionResultGoHome = actionGoHome.execute(); assertEquals("Login failed!","success",actionResultGoHome); } catch (Exception exObj) { logger.error("testGoHomeOK()-Exception:",exObj); fail("testGoHomeOK()-Exception:"+exObj.getMessage()); } } catch (IOException exObj) { logger.error("testGoHomeOK()-IOException:", exObj); fail("testGoHomeOK()-IOException:"+exObj.getMessage()); } catch (SAXException exObj) { logger.error("testGoHomeOK()-SAXException:", exObj); fail("testGoHomeOK()-SAXException:"+exObj.getMessage()); } catch (ServletException exObj) { logger.error("testGoHomeOK()-ServletException", exObj); fail("testGoHomeOK()-ServletException:"+exObj.getMessage()); } }
private void updateCookie(WebClient webClient, WebResponse webResponse) { String[] names = webResponse.getNewCookieNames(); for (int i = 0; i < names.length; i++) { webClient.putCookie(names[i], webResponse .getNewCookieValue(names[i])); } } }
心得: 关于junit或httpunit的测试工程: 需要有suite()方法测试方法名字前面必须有test前缀需要继承junit.framework.TestCase
关于Servlet的session: 获取session:通过ServletUnitClient的getSession方法,可以拿到HttpSession类型的session对象设置session内容:通过HttpSession的setAttribute(name,value)可以设置session里面的内容获取session内容:通过HttpSession的返回Object类型数据的getAttribute(name)可以设置session里面的内容,注:有个返回String类型的getValue(name)也可以,但是已注明过时. 关闭session:当用完session后,可以使用HttpSession.invalidate()方法关闭session。但是这并不是严格要求的。因为,Servlet引擎在一段时间之后,自动关闭seesion。