StrutsTestCase 的 MockStrutsTestCase 和 CactusStruts

TestCactusLoginAction .java 
//  StrutsTestCase - a JUnit extension for testing Struts actions
//  within the context of the ActionServlet.
//  Copyright (C) 2002 Deryl Seale
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the Apache Software License as
//  published by the Apache Software Foundation; either version 1.1
//  of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  Apache Software Foundation Licens for more details.
//
//  You may view the full text here: http://www.apache.org/LICENSE.txt

package examples.cactus;

import java.io.File;

import servletunit.struts.CactusStrutsTestCase;

public class TestCactusLoginAction extends CactusStrutsTestCase {

	public TestCactusLoginAction(String testName) {
		super(testName);
	}

	public void setUp() throws Exception {
		super.setUp();
		setInitParameter("validating", "false"); //添加
		this.setConfigFile("/WEB-INF/struts-config.xml");//添加
	}

	public void testSuccessfulLogin() {
		addRequestParameter("username", "deryl");
		addRequestParameter("password", "radar");
		setRequestPathInfo("/login");
		actionPerform();
		verifyForward("success");
		verifyForwardPath("/main/success.jsp");
		assertEquals("deryl", getSession().getAttribute("authentication"));
		verifyNoActionErrors();
	}

	public void testFailedLogin() {

		setConfigFile("/WEB-INF/struts-config.xml");
		addRequestParameter("username", "deryl");
		addRequestParameter("password", "express");
		setRequestPathInfo("/login.do");
		actionPerform();
		verifyForward("login");
		verifyForwardPath("/login/login.jsp");
		verifyInputForward();
		verifyActionErrors(new String[] { "error.password.mismatch" });
		assertNull(getSession().getAttribute("authentication"));
	}

	public static void main(String[] args) {
		junit.textui.TestRunner.run(TestCactusLoginAction.class);
	}

}

 MockStrutsTestCase

 

//  StrutsTestCase - a JUnit extension for testing Struts actions
//  within the context of the ActionServlet.
//  Copyright (C) 2002 Deryl Seale
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the Apache Software License as
//  published by the Apache Software Foundation; either version 1.1
//  of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  Apache Software Foundation Licens for more details.
//
//  You may view the full text here: http://www.apache.org/LICENSE.txt

package examples;

import java.io.File;

import servletunit.struts.MockStrutsTestCase;

public class TestLoginAction extends MockStrutsTestCase {

	public TestLoginAction(String testName) {
		super(testName);
	}

	public void setUp() throws Exception {
		super.setUp();
		setInitParameter("validating", "false");
		//找到WebRoot和配置文件
		setContextDirectory(new File("WebRoot"));
		this.setConfigFile("/WEB-INF/struts-config.xml");
	}

	public void testSuccessfulLogin() {
		setRequestPathInfo("/login");
		addRequestParameter("username", "deryl");
		addRequestParameter("password", "radar");
		actionPerform();
		verifyForward("success");
		assertEquals("deryl", (String) getSession().getAttribute(
				"authentication"));
		verifyNoActionErrors();

	}

	public void testFailedLogin() {
		setConfigFile("/WEB-INF/struts-config.xml");
		addRequestParameter("username", "deryl");
		addRequestParameter("password", "express");
		setRequestPathInfo("/login");
		actionPerform();
		verifyForward("login");
		verifyForwardPath("/login/login.jsp");
		verifyInputForward();
		verifyActionErrors(new String[] { "error.password.mismatch" });
		assertNull(getSession().getAttribute("authentication"));
	}

	public static void main(String[] args) {
		junit.textui.TestRunner.run(TestLoginAction.class);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值