Testing Servlets with JUnit

If you have ever wondered how you could easily test your servlets with JUnit… Then I have found a very interesting blog entry for you

If you have used commons-httpclient, this is about as simple as it gets, but I still wanted to have a unit test for it. So the problem became, “how can I test this method when it requires that I hit a web site”. After some searching I found that the Jetty servlet-container has a ServletTester class just for this purpose.

I would like to recommend you to read the article to let you test more than you are now.


Tags: , , , , , , , ,

3 Responses to “Testing Servlets with JUnit”

  1. Mark Says:

    I’m not sure what you are trying to achieve, but another alternative is to use the Spring mock objects (e.g. MockHttpServletRequest).

    These objects allow you set parameters etc then pass them into your doXXX methods and the resulting mock response object can be the subject of assertions in your junit tests.

  2. Jonny Says:

    All I want is a set of mock Servlet API objects to test utility classes that __marginally__ depend on Servlet API objects. I don’t need some overly convoluted Apache Cactus framework or unfamiliar and unstructured commons-httpclient that takes forever learn, build, configure or test itself. Too much overhead for a task designed to reduce overhead.

    I am going to check-out these mock objects that Spring offers and see what I come-up with.

  3. Jonny Says:

    Yes, Spring mock classes are precisely what I was looking for.

    Tests Passed: 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: