HttpUnit is an open source test framework for web applications which can provide browser specific behaviour like sending request to the We are using Spring sts , JDK 8, HttpUnit JUnit Tutorial for Unit Testing. 4. However if you’re a programmer who just got the job to create functional or unit tests, you’ll find HttpUnit a much more enjoyable and programmer-friendly toolkit. HttpUnit allows us to test navigating through our JSPs by clicking links and buttons and filling out forms. In this tutorial, we will explain.

Author: Vudotaxe Mezilrajas
Country: Tajikistan
Language: English (Spanish)
Genre: Sex
Published (Last): 9 November 2011
Pages: 135
PDF File Size: 6.50 Mb
ePub File Size: 16.74 Mb
ISBN: 584-1-68034-901-9
Downloads: 51115
Price: Free* [*Free Regsitration Required]
Uploader: Mazugul

You want to write a test that submits your HTML forms and verifies the forms functionality.

HttpUnit Tutorial

Set parameters on the WebForm using its setParameter method. Then simulate clicking a button by asking httpuniy one of the form’s buttons and submitting it using the WebConversation instance.

You fill in form field values using the setParameter method on a WebForm instance. This simulates what the user would do if he was filling out a form in a web browser. You then ask the form for a WebRequest object, passing in the name of one of the submit buttons. All of this is shown in Example The comments in Example explain what is expected at each httpunti.


The overall goal is to ensure that the form treats the email address as a required field. If the field is missing, the form should be redisplayed with an error message. When the form is redisplayed, the name field should be pre-filled with the previously entered value.

Example shows the updated servlet. As is typical in a web application, the validation logic is contained within the servlet, rather than the JSP. Even better, you might want to refactor the validation logic into a helper class rather than the servlet itself.

This step would allow you to write standalone tests against the validation logic without invoking the httpnit. Once the hgtpunit is fully validated, the servlet dispatches to the JSP for rendering. The NewsletterServlet is nearly at its final form.

10x More Productive Blog!

A doPost method bttpunit added to handle the form submission, and the logic formerly found httpunig doGet has been refactored into the dispatchToSubscriptionPage method. This refactoring avoids code duplication and is easily tested with the existing suite of unit tests. Pay particular attention to the todo comments. These indicate that portions of the code are not complete. With the test-first approach taken in this chapter, these pieces of functionality should not be written until the corresponding unit tests are written.


You might also consider putting your todo comments httpumit your test cases, rather than in the code itself. This strategy provides stronger encouragement to focus on test-driven development when those features are eventually added.

Avoid the urge to write all of the functionality at once.

Instead, work on tiny pieces of functionality with each new test. This process htgpunit the likelihood that you will procrastinate and skip some of the tests. Finally, Example shows the revised JSP. The JSP now contains logic to display the error message attribute, which is sometimes provided by nttpunit servlet.

It also pre-fills the value of the name field if necessary. At this point, the tests including all of the old tests should pass. It is also a good idea to try out the web app inside of a web browser to see if you forgot to test anything. The test-first process can continue until all of the functionality is implemented.

Home Programming Java extreme programming.