Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing a Java Servlet

I would like to know what would be the best way to do unit testing of a servlet.

Testing internal methods is not a problem as long as they don't refer to the servlet context, but what about testing the doGet/doPost methods as well as the internal method that refer to the context or make use of session parameters?

Is there a way to do this simply using classical tools such as JUnit, or preferrably TestNG? Did I need to embed a tomcat server or something like that?

like image 400
gizmo Avatar asked Sep 18 '08 08:09

gizmo


People also ask

How to Unit test a servlet?

To properly test a servlet you would either have to run it inside a real servlet container, or create a mock servlet container which can be activated via code, during your unit tests. Either way, it's a bit of work to setup correctly.

What is unit testing in Java?

Unit testing means testing the smaller units of your application, like classes and methods. The reason you test your code is to prove to yourself, and perhaps to the users / clients / customers, that your code works.

What is servlet in advanced Java?

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.

How do you test a lambda expression?

The first is to view the lambda expression as a block of code within its surrounding method. If you take this approach, you should be testing the behavior of the surrounding method, not the lambda expression itself. The only thing that the lambda expression in this body of code does is directly call a core Java method.


1 Answers

Most of the time I test Servlets and JSP's via 'Integration Tests' rather than pure Unit Tests. There are a large number of add-ons for JUnit/TestNG available including:

  • HttpUnit (the oldest and best known, very low level which can be good or bad depending on your needs)
  • HtmlUnit (higher level than HttpUnit, which is better for many projects)
  • JWebUnit (sits on top of other testing tools and tries to simplify them - the one I prefer)
  • WatiJ and Selenium (use your browser to do the testing, which is more heavyweight but realistic)

This is a JWebUnit test for a simple Order Processing Servlet which processes input from the form 'orderEntry.html'. It expects a customer id, a customer name and one or more order items:

public class OrdersPageTest {     private static final String WEBSITE_URL = "http://localhost:8080/demo1";      @Before     public void start() {         webTester = new WebTester();         webTester.setTestingEngineKey(TestingEngineRegistry.TESTING_ENGINE_HTMLUNIT);         webTester.getTestContext().setBaseUrl(WEBSITE_URL);     }     @Test     public void sanity() throws Exception {         webTester.beginAt("/orderEntry.html");         webTester.assertTitleEquals("Order Entry Form");     }     @Test     public void idIsRequired() throws Exception {         webTester.beginAt("/orderEntry.html");         webTester.submit();         webTester.assertTextPresent("ID Missing!");     }     @Test     public void nameIsRequired() throws Exception {         webTester.beginAt("/orderEntry.html");         webTester.setTextField("id","AB12");         webTester.submit();         webTester.assertTextPresent("Name Missing!");     }     @Test     public void validOrderSucceeds() throws Exception {         webTester.beginAt("/orderEntry.html");         webTester.setTextField("id","AB12");         webTester.setTextField("name","Joe Bloggs");          //fill in order line one         webTester.setTextField("lineOneItemNumber", "AA");         webTester.setTextField("lineOneQuantity", "12");         webTester.setTextField("lineOneUnitPrice", "3.4");          //fill in order line two         webTester.setTextField("lineTwoItemNumber", "BB");         webTester.setTextField("lineTwoQuantity", "14");         webTester.setTextField("lineTwoUnitPrice", "5.6");          webTester.submit();         webTester.assertTextPresent("Total: 119.20");     }     private WebTester webTester; } 
like image 126
Garth Gilmour Avatar answered Sep 21 '22 01:09

Garth Gilmour