Using MockServlet: online tutorial
This page will help you set up and use MockServlet.
1 - Installation
First you should download mockservlet.jar, then place it somewhere in your classpath.
There are a couple of things you have to do first.
- Download JUnit. Place
junit.jarin your classpath.- Download
servlet.jar. Place it in your classpath too.Chances are, you already have both of them somewhere in your hard disk.
2 - Description
The core of MockServlet is a set of classes in
org.chenno.mock.servlet. Each of them implements a certain servlet interface, and provides additional functionality to set test values.
MockRequestandMockResponseare the pieces you will surely need. They simulate a servlet request and response, and you use them to calldoGet.The
MockRequestallows you tosetParameter()andsetAttribute(), as key-value pairs. They are internally stored, so when the servlet asks for them, it will retrieve the test values.In contrast, the
MockResponseis a much more passive class: you just specify the file where you want the code stored, and it will redirect to it whatever response is written.You will probably need a
MockConfigto configurate the servlet via theinit()method. It has asetInitParameter()method, that adds the key-value pair to the init parameters.The
MockSessionis also useful: attributes (such as auth objects) can be added at will, to match servlet necessities.3 - Usage
We will assume you know how to use JUnit; you have created a subclass of
TestCase, and are writing yourtestXXX()method. Otherwise, pay a due visit to the JUnit cookbook, here next door in SourceForge.First you create your servlet just as you would any normal class:
SampleServlet servlet = new SampleServlet();Now an optional step: initialize it with a
MockConfig, passing an init parameter.MockConfig config = new MockConfig();
config.setInitParameter("init", "value"); servlet.init(config);Create the
MockRequest, setting all the parameters you need.MockRequest request = new MockRequest();
request.setParameter("param", "value");The
MockResponseis instantiated with the name of the file where the output page will be written:MockResponse response = new MockResponse("output.html");Optionally, you can set up your
MockResponsewithout an output file; a call totoString()will retrieve the output page. Use an empty constructor for it.MockResponse response = new MockResponse();You will probably need some objects stored in the session. No problem: the
MockSessionis there exactly for that purpose.MockSession session = new MockSession(); session.setAttribute("key", sampleObject); request.setSession(session);Now, you're ready to call the servlet using
doGetordoPost, whatever you need.servlet.doGet(request, response);The resulting page will be safely stored in whatever file you specified. You can read it and compare with a stored template, or whatever.
4 - Have fun!
If things aren't working for you, try joining the user list, or contacting the administrator directly. We will be proud to help you.
@version 1.0
@author Alex Fernández