ServletUnit - a good helper for developers

xiaoxiao2021-03-06  69

Extreme Programming (XP) emphasizes unit testing, and push test drive development and minimizes time. In fact, testing is a way of designing, which can be seen as a formal, direct verification design. The test passes, also means that the design is achieved, and the development work is completed. This may be the main reason why it is so important.

So, how do you easily test it? JUnit is the answer to this question. However, it is not so easy to test the program (servlet, jsp, ejb) on the server, because JUnit test is equivalent to running a normal local Java program, and applications on the server often need to run in a container environment. . At this time, two ways can be used for testing: 1. Test directly in the container; 2. Test with an analog environment.

The first way looks very good, but it is often very troublesome to do. Most people's development methods use test case tracking code operation, and want to track the code running in the container, often use a specific IDE development tool to complete. Moreover, once the code is modified, it is often necessary to have a long release / restart service process, and the development efficiency is low.

The second way, that is, the simulated environment test, although there is an unsatpane with the real environment, but its tracking commission is almost all IDE support, and does not need to release services, edit-compilation - test cycle speed, can be large Improve development efficiency, may wish to try.

How to simulate? The author wrote some analog classes such as HTTPSERVLETREQUEST, HTTPSERVLETRESPONSE, and so on until servletUnit. It may not be famous, it is part of a more famous httpUnit. HTTPUnit is mainly designed to "black box test" on the website, while servletUnit can test the server program. The following content answers such a problem:

- How to test a servlet with servletUnit? - How to test access to login and simulate login? How do I test JSP? - SERVLETUNIT can simulate objects and features

* How to test a servlet with servletUnit?

First, you need to create a servlet runtime (it simulates a container) and registers your servlet:

Servletrunner servletrunner = new servletrunner (); // (1) Creating Renager servletrunner.registerServlet ("MyServlet", MyServlet.class.getname ()); // Register your servlet

Then you need to create a unit test customer object:

ServletUnitclient Client = servletrunner.newclient (); // (2) Creating a browser

It is equivalent to a browser, you tell it what you need to browse, this is to use a getMethodWebRequest or PostMethodWebRequest object:

WebRequest WebRequest = New PostMethodWebRequest ("http: // localhost / myservlet"); // (3) Fill in web address WebRequest.SetParameter ("color", "red"); // Add parameters

Finally, you can call your servlet by accessing the webpage specified above: InvocationContext Invocation = Client.newInvocation (WebRequest); // (4) Sending an invocation.getServlet (). Service (invocation.getRequest (), Invocation .getResponse ());

To view the contents of servlet, use a WebResponse object:

WebResponse WebResponse = Invocation.getServletResponse (); // (5) Get the result system.out.print (WebResponse.getText ());

Below is the list of objects used and its usage: servletrunner ----------------- Container ------------ Group Test a servletUnitclient - ------------ Browser ---------- a test case is a WebRequest ------------------ URL Address --------- a time request one (get / postmethodwebrequest) InvocationContext ------------- servlet running environment - a time to ask a WebResponse ------------ --------- Return to the content -------- a time request one

* How to test access to login and simulate login?

First access the login page, then access other pages, use the same servletUnitclient object:

// Initialization 1. ServletRunner and ServletUnitClient ServletRunner servletRunner = new ServletRunner (); servletRunner.registerServlet ( "loginServlet", LoginServlet.class.getName ()); servletRunner.registerServlet ( "myServlet", MyServlet.class.getName ());

ServletUnitclient Client = servletrunner.newclient ();

// 2. Visit the login page WebRequest webRequest = new PostMethodWebRequest ( "http: // localhost / loginServlet"); webRequest.setParameter ( "username", "zhang"); webRequest.setParameter ( "password", "12345"); InvocationContext invocation = client.newInvocation (webRequest); invocation.getServlet () service (invocation.getRequest (), invocation.getResponse ());. WebResponse webResponse = invocation.getServletResponse ();

WebClient_UpdateCookies (Client, WebResponse); // _ VIP: Description

// 3. Access to other pages webRequest = new PostMethodWebRequest ( "http: // localhost / myServlet"); webRequest.setParameter ( "color", "red"); invocation = client.newInvocation (webRequest); invocation.getServlet () .Service (Invocation.getRequest (), Invocation.getResponse (); WebResponse = invocation.getServletResponse (); what to explain, in order to use the same HTTPSession object as the login page when accessing other pages, you need to update it after logging in. Our unit tests the cookie of the customer object, call the following method:

static void WebClient_updateCookies (WebClient client, WebResponse webResponse) {// This piece of code reference WebClient.updateCookies String [] names = webResponse.getNewCookieNames (); for (int i = 0; i

* How to test JSP?

First compile JSP to servlet and then access it.

* ServletUnit able to simulate objects and functions: - HttpServletRequest: getSession: getServletContext: getRequestDispatcher: get / setAttribute- HttpServletResponse- HttpSession- ServletContext: getResource: getResourceAsStream: get / setAttribute

The above is not listed all supported features, but from here you can see that almost all common functions can be simulated. Need to pay attention to a few points: 1) Several SetAttribute methods do not support null value 2) To get local resources with servletContext.getResource methods, you must construct a servletrunner object in the way in New Servletrunner (WebXMLFILESPEC, ContextPath, for example::

String userdir = system.getProperty ("user.dir"); userdir = "//mywebapp/Web-inf/web.xml"; servletrunner = new servletrunner (userdir, ");

Such analog ServletContext will load other resources based on the location of our designated web.xml file. If web.xml is large, the speed will be very slow, we can write an empty profile web_blank.xml:

Place this file in Web.xml, and then change the above UserDir to point to Web_Blank. XML can be.

ServletUnit does not simulate these features: - httpservletRequest.isRequestedSessionIdValid () You can use null == httpservletRequest.getSession (false) to determine if the session is valid - JNDI Find (Data Source, EJB, etc.) requires some parties to find data sources, usually this needs to be modified Existing code, plus a debug status variable, runtime judgment If you are in the debug state, you will get database connections through JDBC.

* Reference and reference

Integrated test using HTTPUNIT (Xiao Jing): http://gceclub.sun.com.cn/staticcontent/html/2004-03-30/httpunit.html (need to register)

The content of this article is suitable for httpunit 1.5.4

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

New Post(0)