Test Struts Action class using Strutstestcase

xiaoxiao2021-03-06  78

Test Struts Action class using Strutstestcase

One. Foreword

For OpeadP project, the UT phase is originally intended to use HTTPUnit to test the relevant Action class. After investigation, I found that StrutstestCase is more suitable because HTTPUnit requires the procedures tested first, and StrutstestCase can be independently tested from the corresponding container, which is more convenient for UT testing.

HTTPUNIT Principle: httpUnit The Page Framework (FRAMES), Cookies, and the like are processed by analog browser behavior. With the function provided by HTTPUnit, you can interact with the server side, which will be processed as a common text, XML DOM object, or a collection of links, page frames, images, forms, tables, etc., and then use the JUnit framework Test, you can also guide a new page, then processes the new page, this feature allows you to handle a group in an operation chain.

two. StrutstestCase Introduction

StrutstestCase is an open source project of SourceForge.org, which is based on JUnit's convenient test framework for Struts. It provides two ways to simulate objects and CACTUS to "real" running struts an ActionServlet, allowing your Struts code to be tested without starting the Servlet engine.

Usually the test server-side code has two comparison commonly used test methods:

1. Mouck Objects it reaches test results by assuming server-side containers;

2. In-container testing, it is a test effect in a real container;

StrutstestCase offers two base classes (JUnit Testcase that inherited the standard):

Mockstrutstestcase:

Through the name, you can know that he uses the first middle method without launching the servlet. Some httpservlet implementations assume the container environment.

CACTUSSTRUTSTESTSTSE:

It is reflected in the container test (real environment test), which tests Struts code through another test frame (Cactus Testing Framework: http://jakarta.apache.org/cactus).

three. Use strutstestcase

In the UT phase, the "Imitation Object" method is used to test the Action class.

The following is briefly introduced to use the StrutstestCase using an example of logging in to the Struts framework user.

Suppose the LoginAction code responsible for the user login is as follows:

Package strutstestcase;

Import org.apache.struts.action. *;

Import javax.servlet.http. *;

Public Class LoginAction

EXTENDS ACTION {

Public ActionForward Execute (ActionMapping Mapping,

Actionform Form,

HTTPSERVLETREQUEST REQUEST,

HttpservletResponse httpservletResponse) {

String username = (loginform) form .getusername ();

String password = (loginform) form .getpassword (); actionerrors errors = new actionerrors ();

IF ((! ") || (! Password.equals (" yang "))))))))

Errors.Add ("Password", New ActionError ("Error.Password.mismatch));

IF (! errors.isempty ()) {

SaveerRors (Request, Errors);

Return New ActionForward (mapping.getinput ());

}

HttpSession session = request.getations ();

Session.SetaTRibute ("Authentication", UserName;

// forward control to the specified surcess URI

Return mapping.findforward ("Success");

}

}

Other loginform code, login JSP page, web.xml, struts-config.xml will not be described here again, see attachments.

Let's write the LoginAction's test class LoginActionTest:

Package strutstestcase;

Import servletUnit.struts. *;

Import junit.framework. *;

Import java.io.file;

// Test class to inherit the MockStrutstestcase class (also subclass of the TestCase class)

Public class testloginaction extends mockstrutstestcase {

Public TestLoginAction (String Testname) {

Super (testname);

}

Public void setup () throws exception {

Super.setup ();

/ / Do some of the initialization work, you must let the test class find web.xml, struts-config.xml, it is best to set the context directory

THIS.SETCONTEXTDIRECTORY (New File ("E: / EXERCISE / Project / Strutstestcase / Logindemo /"));

SetinitParameter ("Validating", "False");

}

Public void testsuccessfullogin () {

/ / According to struts-config.xml, set the actionMapping to be executed.

SetRequestPathInfo ("/ login");

/ / Submit the data to be submitted when Submit

AddRequestParameter ("UserName", "YANG");

AddRequestParameter ("Password", "YANG");

/ / Execute an Execute () method of the Action class

ActionPerform ();

// Verify that the return is correct

VerifyForward ("Success");

VerifyForwardPath ("/ success.jsp");

// Verify the content in the session

Assertequals ("yang", getsession (). getAttribute ("Authentication")); // Verify that there is Error

VerifynoActionerrors ();

}

Public void testfailedlogin () {

AddRequestParameter ("UserName", "Haha");

AddRequestParameter ("Password", "Passwd");

SetRequestPathInfo ("/ login");

ActionPerform ();

VerifyForward ("Login");

VerifyForwardPath ("/ login.jsp");

VerifyInputForward ();

VerifyActionerRRORS (New String [] {"Error.Password.mismatch"});

ASSERTNULL (GetSession (). GetAttribute ("Authentication"));

}

Public static void main (String [] args) {

Junit.textui.teStrunner.Run (TestLoginAction.class);

}

}

转载请注明原文地址:https://www.9cbs.com/read-93542.html

New Post(0)