StrutstestCase application based on JUnit 2.0

zhaozj2021-02-12  185

StrutstestCase application based on JUnit 2.0

In my previous document "The development of test driver" said that I have to write some documents in the test framework application, today I want to achieve one of my promises, this article is introducing strutstesecase, familiar with Developers who use Struts have had such a haunt: How do my Action test? (Do not say "test first"?), If there is no viable test frame, where is the Struts environment going to simulate (convenient, transparent to simulation)? Don't worry, the next part I want to introduce you to the test framework that meets our requirements: Strutstestcae.

- Written in front

Main content introduction:

1. What is strutstesecase?

2. Where is its "home"?

3. How to make it work for us? (With the description: I do this implement "test first"?)

4. Compatible with Struts1.1 Developer

5. Reference resource

"Because Struts and JUnit entry are involved here, I assume that the readers of this article are developers with Struts development experience and are familiar with JUnit."

The first part: What is Strutstestcase?

StrutstestCase is a Test Framework for Test the Struts Framework based on JUnit. It provides two ways of simulation objects (Mock Object) and CACTUS to "real" running struts an ActionServlet, which allows you to test your Struts code without starting the Servlet engine. Because StrutstestCase can test your code with ActionServlet, it doesn't light you can test your Action, and it can also test your (container?) Mapping, frombeans, and forwards declarations. I have mentioned that it is "transparent" for us developers, because it is like action, mapping, form beans, and forward, we really can really like in our test code as in the regular XXXAction. Use them.

In the latest release, it also provides tests for Tiles and Multi-Modules (Functions in Struts1.1).

Wow, is it very wonderful, don't worry, we can quickly enjoy it.

Part II: Where is its "home"?

Just like the perceived open source project, Strutstestcase's home is also in "SourceForge.org" (our great sourceforge is just like a busy peak slot, you can pass http://sourceforge.net/project/showfiles .php? group_id = 39190 to download it to the latest version.

Javadoc: http://strutstestcase.sourceforge.net/api/index.html

Hot Forum: http://sourceforge.net/forum/forum.php? Forum_id = 121751

FAQ: http://strutstestcase.sourceforge.net/faq.htm

Part II: How to make it work for us?

"Mock Testing) VS Container Test (in-Container Testing"

Usually the test server-side code has two comparison commonly used test methods: mock objects (Mock Objects) it achieves the test effect by assuming server-side containers;

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

And our StrutstestCase can play two roles, respectively, can play two roles, respectively, with minimal impact on your test code. So we have to say how the two implementations have been completed?

StrutstestCase provides two base classes (they inherit the standard JUnit Testcase):

Mockstrutstestcase:

Through the name, you can also know that he is a condition that the HTTPSERVLET implementation assumes the container environment by the first middle method without starting the servlet.

CACTUSSTRUTSTESTSTSE:

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

PS: The code involved in this article is completed through the first middle method (inherited mockstrutstestcase), you want to use CactusSteStesecase as long as you simply let the test code inherit CactusSteseSecase.

Below we focus on MockstrutstestCase works for us?

First let's take a look at a simplified code of a simple loginaction:

Public class loginaction extends action {

Public ActionForward Perform (ActionMapping Mapping, Actionform Form, HttpservletRequest Request, HttpservletResponse Response) {

String username = (loginform) form .getuserName (); string password = ((loginform) form .getpassword ();

ActionerroS Errors = new actionerrors ();

IF ((! ")) || ("))) Errors.Add ("Password", New Actionerror ("Error.Password.Mismatch));

IF (! errors.empty ()) {saveError (Request, Errors); return mapping.findforward ("login");}

// Store Authentication Info on the session httpsession session = request.getations (); session.setttribute ("Authentication", UserName

// forward control to the specified success uri return mapping.findforward ("success");}

LoginAction completed a simple landing intent, collect login data (username and password) from the client, then make a verification, if the verification is incorrectly returned to the login page; if the login successfully returns a successful page (or business work platform) and put the name of the user Into the session.

Then we will start with this simple program from the upper side:

First, we should create a test case TestLoginAction, and its basic shelf is like this:

(Please remember that the code of Loginaction at this time you haven't writted, and the actionmapping of loginaction in struts_config.xml is not there. These things we have to write while testing, but must write test, God, what I haven't tested it yet, don't worry, and look down, j)

Public class testloginaction extends mockstrutstestcase {

Public void setup () {super.setup ();

Public void teardown () {super.teardown ();

Public TestLoginAction (String Testname) {Super (Testname);

Public void testsuCcessfullogin () {}}

First of all, our mind has such an actionmapping (note only assumptions):

With such a hypothesis, we can start from the test code:

Public class testloginaction extends mockstrutstestcase {

Public void setup () {super.setup (); // Do some initialization, such as database connections, etc.

Public void teardown () {super.teardown (); // Close the resources you open in Setup, such as turning off database connections}

Public TestLoginAction (String Testname) {Super (Testname);

Public void testsuccessfullogin () {

// Which anctions you want to do? This is useful to use the hypothesis that we have just made. SetRequestPathInfo ("/ login");

// First, you should initialize the submission data: user name, password this.addRequestParameter ("USRename", "JPLATEAU"); this.addRequestParameter ("passwd", "Sandy"); // Note that you can The username and password data after the submission is used, and I will explain it below //, I will explain it. After the initialization data is completed, I will start executing Execute () in the action, very simple, use ActionPerform ()

This. ActionPerform ();

// Is it true to return? Here, use our assumption material this VerifyForward ("Success");

/ / Is there a user name in Session after the success of the Successful Success? String expect_username_from_session = "jplateau"; // The StrutStestCase can be used in the test code using the same environment as in the test code, such as the corresponding actionform // can be obtained by this.getActionform (). This.getRequest () get httpRequest // can get httpsession with THISGETSESSION (), as follows: string actual_username_from_session = this.getations (). GetAttribute ("Authentication");

This. Assertequals ("", expect_username_from_session, actual_username_from_session);

Public void testfailurelogin () {// Test of login failed is not written here

}

The upper side is a simple test code, good, run! He is sure that, first, our Java code has not been written yet. Struts-config.xml has not yet, then now you can do these things now: "Use the simplest practice or code to make the test pass the test through the test "

Part III: Compatible with Struts1.1 Developer

Here, write and struts1.1 related content: Test Tiles and Multi-Modules!

Test Tiles

Suppose the situation in our actionmaping such as Forward to Tiles, as follows:

/ / Here is the use of Tiles in Struts, please refer to the relevant information

At the same time, there is a corresponding configuration in TILES_DEFS.XML:

//, Mainlayout is my type of type defined in Tiles

Then we can test Tiles as follows in the test code.

// this. VerifyTilesForward ("Success", "UserListLayout");

Test multiple modules:

About multi-module test I hope to give you a probably introduction in the comment section below the code:

Public Class TestloginAction Extends Mockstrutstestcase {public testname {super (testname);}

Public Void TestSuccessFullogin () {// "MyModule" is the name of a separate module in the system setConfigfile ("MyModule", "/ Web-INF / STRUTS-Config-mymodule.xml");

// The example of this place and the previous single module has some differences. There are two parameters here. The first parameter is the module // name, the meaning of the second parameter is the same; // and the single module Different from both sides This.SetRequestPathinfo ("/ mymodule", "/ login.do"); this.addRequestParameter ("UserName", "JPLATEAU"); this.AddRequestParameter ("Password", "Sandy"); this.ActionPerform (); This.VerifyForward ("Success");

String expect_username_from_session = "jplateau";

String actual_username_from_session = this.getations (). GetAttribute ("Authentication");

This. Assertequals ("", expect_username_from_session, actual_username_from_session);}}

Reference resources:

1. Http://strutstestcase.sourceforge.net

(This article is basically the idea of ​​using this document, English is good to read this document.)

2. Kent Beck "Test-Driven Development By Example"

3. This article is started in our information site: http://plateau.sicool.com

JPLATEAU Wednesday, November 12, 2003, written in Guangzhou Jingbo

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

New Post(0)