StrutstestCase introduction

xiaoxiao2021-03-06  85

StrutstestCase introduction

Author: Lonely Firefly

Overview

StrutstestCase for Junit is an extension of the JUnit TestCase class, providing a Struts framework based code testing device. StrutstestCase also provides the Mock object method and the CACTUS method to actually run the struts actionservlet, you can test it or pass it by running the Servlet engine. Because StrutStestCase uses the ActionServlet controller to test your code, you can not only test the implementation of the Action object, but also test mappings, from beans, and forwards declarations.

Test the StrutsTestCase to test the Struts application (container external test) also belongs to the Mock object test, but unlike EasyMock, EasyMock is an API that creates a Mock object, and StrutStest is specifically responsible for testing struts applications. Mock object test frame.

In addition to the container test, StrutStest can also be very convenient to test the test in the container.

The current version of Strutstest2.1.2 is Strutstest2.1.2, supports Servlets 2.2 and 2.3 specification and supports Struts 1.1, CACTUS1.6.1 and JUnit 3.8.1. Struts1.0 is not supported in this release. If you need to test the Struts1.0 application, the corresponding version is Strutstest2.0. You can download from http://sourceforge.net/projects/strutstestcase/.

2. How to use

First, you should download this tool from the StrutstestCase home page, there will be a JAR file after decompression, and an example and API DOC. When writing StrutStestCase, you need to introduce this JAR file into the project (put together with the JAR file in Struts), and since it is an extension to JUnit, you must also introduce JUnit.jar into the project.

There have been two test methods that have been described above, and the following is briefly description:

1) MockStrutstestCase (Mock Object Test): Tests by analog containers. MockStrutStestcase Analog container environments with a set of HTTPSERVLET Mock objects without running a Servlet engine.

2) CACTUSSTRUTSTESTSTSE (In-Container Testing): Testing in real-running containers by using the CACTUS test framework.

Note: The above two classes are extensions to JUnit TestCase. If you want to change the Mock object test method to the CACTUS method, you only need to change the parent class of our defined subclass by MockStrutStestCase to CACTUSSTRUTSTESTSESE, without changing internal code.

Naming Rules: There is no strict specification for the name of the name, but personal recommendations can be used for the Action name to be tested; however, it is possible to note that the system will only run the method named "Test". Test.

By following the instance I wrote, you will explain the specific usage method, first look at the code in an action (Registeraction.java):

Package com.firefly;

Import org.apache.struts.action. *;

Import javax.servlet.http. *; / **

* @Author Firefly

* /

Public class registeraction extends action {

Public ActionForward Execute (ActionMapping Mapping, Actionform Form,

HTTPSERVLETREQUEST REQ, HTTPSERVLETRESPONSE RES) {

Registerform rf = (registerform) form;

// String username = rf.getusername ();

String password1 = rf.getpassword1 ();

String password2 = rf.getpassword2 ();

IF (Password1.Equals (password2)) {

Try {

Return mapping.findforward ("Success");

}

Catch (Exception E) {

Return mapping.findforward ("failure");

}

}

Return mapping.findforward ("failure");

}

}

The function is very simple, that is, the password two inputs are the same, the same, go to success, otherwise go to Failure. Here is the test code (TestRegisterAction.java):

Package com.firefly;

Import servletUnit.struts. *;

/ **

* @Author Firefly

* /

Public Class TestregisterAction Extends Mockstrutstestcase {

Public void setup () throws exception {

Super.setup ();

}

Public void Teardown () throws exception {

Super.teardown ();

}

Public TestRegisterction (String Testname) {

Super (testname);

}

Public void testsuccessfullogin () {

SetRequestPathInfo ("/ register");

AddRequestParameter ("UserName", "Hellking");

AddRequestParameter ("Password1", "123");

AddRequestParameter ("Password2", "123");

ActionPerform ();

VerifyForward ("Success");

}

Public void testfailedlogin () {

SetRequestPathInfo ("/ register");

AddRequestParameter ("UserName", "Hellking");

AddRequestParameter ("Password1", "123");

AddRequestParameter ("Password2", ");

ActionPerform ();

VerifyForward ("failure");

}

}

Description:

1) If Override has setup () and teardown () methods, you must display the corresponding method of calling its parent class in the first line. The reason is simple, the parent class defines a lot of content in these two methods to ensure that the program can follow The established rules are executed, so we must load it. 2) In the test method, the first thing we need to do is to inform Struts which map is used in this test, we specify a path associated with struts mapping, which is used in the Struts label method. The mechanism is the same.

3) We will then do it through the Request object to pass the FORM BEAN attribute, which is the same as Struts.

4) When it is running an action, we complete the task by using the actionPerform () method.

5) The last thing to test is whether the program operation results are the same as what we expect, by calling the verifyForward () method to confirm. Other methods are also available here, such as: assertequals (), assertnull (), verifynoactionerrors (), verifyActionerRors (), and more.

6) Even if you do not perform any tests in the Test method, the system will not prompt any errors (this execute () method in this with action must return ActionMApping varying, the displayed test results are still successful. You can write multiple tests in a Test method, but individuals do not recommend this, which will bring great trouble to the naming of the method and the structure of the program. In addition, it will return immediately after the test failure is encountered, not to continue to run down (this is quite similar to the AND statement).

7) Main functions and roles:

SetRequestPathInfo, set request for Request

AddRequestParameter, add parameters and corresponding values ​​to Request

ActionPerform, execute this request

VerifyForward, verify that the name of Forward is correct

VerifyForwardPath, verify that the forward Path is correct

VerifyNoActionerRRORS, verify that there is no ActionError during action execution

VerifyActionerRors, verify the contents of ActionError collection generated during action execution

For details, please check the API DOC.

Here are some reference websites: strutstestcase home: http://sourceforge.net/projects/strutstestcase/ API Doc: http://strutstestcase.sourceforge.net/api/index.html

If you need to learn more about Mock Objects and CACTUS test frameworks, you can go to its official website: Mock Objects: http://www.mockobjects.com/COCTUS: http:/cactus/index.html

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

New Post(0)