Software test technology of J2EE development platform
Foreword - The emerging concept of the test-oriented software development process software development process is the role of software testing, enhances the important task of continuous and repeatedly when the system is developed, ensuring that every stage can be found early. The risk factor hidden within the system. When the test results of a certain phase cannot reach the expected requirements, they must be traced back to previous development phases, analyze and review again, this process is refactoring. The mechanism of cooperation with reconstruction allows the system to grow under strict test monitoring. However, due to the rise of the revolution in the network era, the architecture of the software system becomes more complex, and the development of relative software tests is more important.
The software test XP in the J2EE platform defines the levels of two tests. The first level is "unit test" because the intention of the unit test is to verify whether the program code is logical, and is a module inside the system. Therefore, it can be called a program logic testing. In order to develop a special architecture of different application development platforms, in the J2EE's platform, Integration Testing is developed closely with J2EE Container.
Another level is an acceptance test, also known as functional testing. In software testing, there is also a performance test (Performance Testing). Since the demand for efficacy testing and customer is inseparable, it is classified as an extended application of the acceptance test. Finally, it is summarized, and a J2EE platform may require the test process, as well as the relationship between the test, as shown in the figure.
In the web layer software test concept and process, as shown in Figure 2, the number symbol next to the keyhead represents the execution step of the entire test process. First prepare the measured data and the measured system, take REQUEST by virtual browser, and obtain the response to the test system to wrap HTML CODE. Then use the test platform to assist us for the comparison of the measured data and the expected value. When the result of the comparative reward is correct, the function of the test system represents it is functioning properly. The test platform in the figure plays a key role in the test in the test.
Although the concept of development test platform has been a year, there are not many test platforms with extendable and open architecture, where the OpenSource community is a representative test platform developed by JAVA.
1 Unit Test Platform - The design architecture of the JUnit Junit platform is a critical constituent architecture that uses two design modes of commands (Composite ", DESIGN PATTERN). The core category in the JUnit platform is TestCase, and each TestCase represents a command object. TestCase contains several Test Method to test whether the output object of the PUBLIC METHOD in the test class is the same as the expected result. There are several ASSERT METHODs to assist alignment within the JUnit platform.
Another core category of the JUnit platform is TestSuite, and each TestSuite represents a composite object. A TestSuite can consist of several TestCase or several TestSuite, so you can patch up a number of TestSuite according to the requirements of the test. The entire JUnit test platform constituting architecture is shown in Figure 3. After understanding the architecture of the JUnit platform, we can use the JUnit platform to develop the integrated test and functional test of the test system.
Code instance:
Import junit.framework. *; import java.util.vector;
Public class vectortest extends testcase {
Protected vector fempty;
Protected Vector Ffull;
Public vectortest (string name) {
Super (Name);
}
Public static void main (String [] args) {
Junit.textui.teStrunner.run (Suite ());
}
protected void setup () {
Fempty = new vector ();
Ffull = new vector ();
FFull.AddeElement (New Integer (1));
FFull.AddeElement (New Integer (2));
FFull.AddeElement (New Integer (3));
}
Public static test suite () {
Return New TestSuite (Vectortest.class);
}
Public void testcapacity () {
INT size = ffull.size ();
For (int i = 0; i <100; i )
FFull.AddeElement (New Integer (i));
Asserttrue (ffull.size () == 100 size);
}
Public void testclone () {
Vector clone = (vector) ffull.clone ();
Asserttrue (clone.size () == ffull.size ());
Asserttrue (Clone.Contains);
}
Public void testcontains () {
Asserttrue (FFull.Contains);
Asserttrue (! fempty.contains);
}
Public void testlementat () {
Integer i = (Integer) ffull.efficient (0);
Asserttrue (I.intValue () == 1);
Try {
Integer J = (Integer) ffull.ementat (ffull.size ());
} catch (arrayindexoutofboundsexception e) {
Return;
}
Fail ("Should Raise An ArrayIndexOutofboundSexception";
}
Public void testremoveall () {
FFull.RemoveAllelements ();
Fempty.removeAllelements ();
Asserttrue (ffull.isempty ());
Asserttrue (fempty.isempty ());
}
Public void testRemoveElelement () {
FFull.RemoveElement (New Integer (3));
Asserttrue (! ffull.contains);
}
}
Import junit.framework. *;
Import junit.runner.baseteStrunner;
Public class alltests {
Public static void main (String [] args) {
Junit.textui.teStrunner.run (suite ());}
Public static test suite () {
Testsuite Suite = New Testsuite ("Framework Tests");
Suite.addTestSuite (ExtensityTest.class);
Suite.addTestSuite (Testcasetest.class);
Suite.addtest (Suitest.suite ()); Suite.addTestSuite (ExceptionTestCasetest.class);
Suite.addtestsuite (Testlistenertest.class);
Suite.addTestSuite (ActiveTestTest.class);
Suite.addTestSuite (assertTest.class);
Suite.addTestSuite (Stackfiltertest.class);
Suite.addTestSuite (sortertest.class);
Suite.addTestSuite (REPEATEDTESTEST.CLASS);
Suite.addTestSuite (Testimplementortest.class);
IF (! baseStrunner.invajava ()) {
Suite.addTestSuite (Textrunnertest.class);
IF (! isjdk11 ())
Suite.addtest (New TestSuite (TestcaseClassLoaderTest.class);
}
Return suite;
}
Static boolean isjdk11 () {
String Version = System.getProperty ("java.version");
Return Version.StartSwith ("1.1");
}
}
2 Connected Test The Connection Test provides the J2EE Container environment with the CACTUS application integration test, which can quickly and easily verify that the interactive behavior of Domain Object and J2EE Container is logical. Therefore, the object of the integrated test is the basic unit with a program code of an EJB, Servlet, or JSP. The sub-plan of the Jakarta program in the Open Source community is born in order to integrate the test. CACTUS is basically the development of the JUnit platform, so in addition to the original basic Method, it also provides a beingxxx () and endxxx () and endxxxx () and endxxxx () of the browser's internal behavior. These two Method come to these Method execution sequences and behavioral patterns interact with Web Container, as shown in Figure 4.
We use Beginxxx () to set the string parameters to pass to the test object. After executing Beginxxx (), it will issue request to transfer the parameter name to the parameter value to the web container. TestCase executes setup (), establishes the object environment required for the test object, followed by performing the action of the test object in Testxxx (). When the action of the access to the test object is executed, you can verify that the test object may be stored in the session. Then the Web Container will perform the action of the release resource, then return the Response to the Client side. Finally, Endxxx () is executed in the client end, which is also the end of the integrated test when performing ENDXXX () when executing the HTML Code and the expected value. Remitting the functions performed by these five Method as shown in Table.
Although the CACTUS architecture provides the comparison function of the test target output and the expected result, it is not conducive to the comparison when the content of the return HTML Code is too large. Therefore, a practical approach is used. This approach is to add an ID in the critical volume label in the critical volume of the HTML Code to produce the Program Code of the HTML Code to be output at the JSP or Servlet. When Endxxx () is to be compared, read the external data file recorded with the ID attribute value and the expected value, and then obtain HTML CODE to the access mechanism of the DOM, it is possible to quickly compare the critical data. Not only can the alignment work modular, but also change the expected value at any time without the need to recompile the test code. If readers have similar problems, they may wish to solve the same strategy. Integrated tests are different from unit testing, although the difficulty of writing test codes is reduced, but also because Domain Object is combined with J2EE Container, and cannot provide a simple test environment for Domain Object. Therefore, if other tests can be simply verifying the entire system, it can make up for the shortcomings of integrated tests. Functional test is a role that plays such a way.
Configuration information and code instance:
Org.apache.cactus.server.servlettestRedirector
servlet-class>
init-param>
servlet>
Org.apache.cactus.server.runner.ServletTestrunner
servlet-class>
servlet>
servlet-maping>
servlet-maping>
Example Testcase:
Import junit.framework.test;
Import junit.framework.testsuite;
Import org.apache.cactus.servlettestcase;
Import org.apache.cactus.WebRequest;
Public Class TestsampleServlet Extends Servlettestcase
{
Public TestsampleServlet (String thename)
{
Super (thename);
}
Public Static Test Suite ()
{
Return New TestSuite (TestsampleServlet.class);
Public Void BeginsavetoseessionOk (WebRequest WebRequest)
{
WebRequest.addparameter ("TestParam", "IT Works!");
}
Public void testsavetoseession ionok ()
{
SampleServlet servlet = new SampleServlet ();
servlet.savetosesis (Request);
Assertequals ("IT Works!", Session.getattribute ("testattribute");
}
}
3 Business logic testing and strutstestcase application
The use of Struts is getting wider, but there is no suitable test framework. CAACTUS, HTTPUNIT although you can test JSP, servlet, but the test based on the Struts framework is still more troublesome.
However, how many problems in StrutstestCase have solved some problems, and the following mainly talks about the application of StrutstestCase. Like other JSP / Servlet test frameworks, StrutStestcase also has two test structures, one is the MOCK structure, and the other is the structure of CACTUS.
Because StrutStestCase is also developed on JUnit, it is similar to JUnit. You can refer to his example and API.
It is a pity that there is no Ant in the StrutstestCase example, so its operation is more headache, especially the processing of the configuration file. StrutstestCase must be able to find struts-config.xml and web.xml files at runtime, the default location is the web-inf / directory, so the web-inflicate is existed in classpath or specified when running. For example: My Struts-Config.xml and Web.xml are located in D: / Struts-Test / WebApp / Web-INF / STRUTS-TEST / WebApp / Web-Inf / STRUTS-TEST / WebApp / Web-INF / The Struts-Test / WebApp path is placed in the classpath. If my Struts-config.xml is not in the / web-inflicment, but in / web-inf / conf / directory, then the setConfigfile (String path) method is required, according to the situation just now,
public void testSuccessfulLogin () {setRequestPathInfo ( "/ login"); addRequestParameter ( "username", "deryl"); addRequestParameter ( "password", "radar"); actionPerform (); verifyForward ( "success");}
public void testFailedLogin () {addRequestParameter ( "username", "deryl"); addRequestParameter ( "password", "express"); setRequestPathInfo ( "/ login"); actionPerform (); verifyForward ( "login");} public void Setup () throws exception {super.setup (); setconfigfile ("/ web-inf / conf / struts-config.xml); 4 functional testing concept with HTTPUnit Application with UML terminology, functional test object It is the behavior specified by USE CASE, whether the test system meets the required function, can it achieve the user's needs? The object of the unit test is the relationship and behavior described by the Test Object Classes Diagram and Sequence Diagram, whether the test unit is executed correctly, does it meet the program logic? . Whenever a phased function test is completed, a part of the system is completed.
The HTTPUNIT API kit of the Open Source community is developed for functional testing. HttpUnit is a virtual browser written in Java to simulate internal behavior of the browser. The CACTUS inspection of the CACTUS mentioned in the previous section is also completed by httpUnit.
In addition, HTTPUnit can also comply with the JUnit platform to verify whether the feedback page is in line with the expected results. The operation mechanism of the HTTPUnit platform is to construct the Request signal that is packaged in the form of an object by simulating the user by simulating the user, and send it to the destination website, and then wait until the website is handled. After this Request, the Response signal encapsulated in the object form will be passed back to HTTPUnit.
Since HTTPUnit receives the RESPONSE object of the standard HTTP protocol, no matter whether the site is a static web language or in any dynamic server-side language, you can simulate a website browse, and obtain a standard HTML code. . On the market, there are also pre-quasi-playback software for functional testing. You can record the steps browsing in advance, then repeatedly play the pre-recorded process, and finally return the data to the test personnel, for the test personnel to analyze, greatly The burden of the test code. However, such software has the following shortcomings: 1. The higher the complexity of the site design, the more branching the branch process, the pre-recorded process is unable to make effective modular management. 2. Although the pre-recording play software has a Script or XML file that provides a record browse step, although these instruction codes can be reused, if an effective detection mechanism is unable to provide an error, it is easy to expect. Error production. 3. Tester needs to re-learn the script language that is exclusive to the pre-quotation software or an XML file syntax, which cannot be written from both familiar program languages to browse the website. 4. When the website's operating interface is often added or modifying the original interactive design, the new browsing website must be retrieved without re-use.
HTTPUNIT solves the troubles of software developers. HTTPUNIT is a test tool in a black box operation, so we only need to focus on how to write a test code for analog browser behavior on the JUnit platform. Method execution in HTTPUNIT and behavior mode interaction with Web Container, as shown in Figure 5, the numbers indicated by arrow symbols, represents the order of execution of these Method. When we set up a test URL with the relevant environment, setup () will execute an action to the test website. When the access action is completed, the Response is retracted to the Client side. At this point, you can do HTML CODE and expected value comparison work in Testxxxx (). Finally, the action of the release resource is performed in the Client side, and the completion of the functional test is also completed when performing Teardown (). The functions performed by these three Method are shown in Table 2.
Although HTTPUnit provides powerful simulation function, HTTPUNIT itself still has two disadvantages. First, when HTTPUnit is tested in conjunction with the JUnit platform, since HTTPUnit Access HTML Code is too close to the file structure within HTML, it is also necessary to modify the corresponding test code when the web version needs to change. For such troubles, the same improvement strategy for detecting HTML CODE with CACTUS is used to achieve a rapid alignment without adjusting the test code. Second, when HTTPUNIT is combined with the JUnit platform, when the JUnit special operation mechanism cannot be remembered, every time you have already viewed URL, so when a test URL is strong with other URLs, To return to the correct browsing status, you must reach with recursive memory. For example, to access the second page, you must remember the status of the first web page. Access the third page to remember the status of the first and second web pages, you need to remember when accessing the nth page. The first web page to N-1 web page, this way is not easy to modify the test code, as shown in Figure 6.
Therefore, HTTPUnit itself can also be written to the characteristics of the program code that operate independently, written into an emulator that browsing the website step. Then use JUnit's setup () to access the browsing status of the test website, you can make a comparison work in TestXxx () to obtain the correct web page status. After modular HTTPUnit test architecture is shown in Figure 7.
5 You can refer to test items: An example of a relatively simple test case in the JUnit package.
Eclipse development platform JUnit Plugin Tests and Automated Testing Framework plug-in JUnitTest project example, more detailed introduction to the JUnit test platform.
Reference 1. JUnit official website http://www.junit.org/index.htm2. Cactus official website http://jakarta.apache.org/cactus/index.html3. Strutstestcase website http: //strutstestcase.sourceForge. Net / 4. httpunit website http://sourceforge.net/projects/httpunit/5. Eclipse website http://www.eclipse.org/downloads/index.php