Required easyMock.jar file
download from http://www.easymock.org/Downloads.html
our servlet class
NameServlet.java:
-------------------
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NameServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String firstName = req.getParameter("firstName");
String lastName = req.getParameter("lastName");
if (firstName != null) req.setAttribute("firstName", firstName);
if (lastName != null) req.setAttribute("lastName", lastName);
}
}
test class for above class
NameServletTest.java:
-----------------------
import static org.easymock.EasyMock.*;
import javax.servlet.http.HttpServletRequest;
import junit.framework.TestCase;
//other imports omitted
public class NameServletTest extends TestCase
{
public void testWithMockObjects() throws Exception
{
// strict mock forces you to specify the correct order of method calls
HttpServletRequest request = createStrictMock(HttpServletRequest.class);
expect(request.getParameter("firstName")).andReturn("laxman");
expect(request.getParameter("lastName")).andReturn("asman");
request.setAttribute("firstName", "laxman");
request.setAttribute("lastName", "asman");
//unexpected method calls after this point will throw exceptions
replay(request);
new NameServlet().doPost(request, null);
//check that the behaviour expected is actually
verify(request);
}
//more test methods omitted ...
}
download from http://www.easymock.org/Downloads.html
our servlet class
NameServlet.java:
-------------------
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NameServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String firstName = req.getParameter("firstName");
String lastName = req.getParameter("lastName");
if (firstName != null) req.setAttribute("firstName", firstName);
if (lastName != null) req.setAttribute("lastName", lastName);
}
}
test class for above class
NameServletTest.java:
-----------------------
import static org.easymock.EasyMock.*;
import javax.servlet.http.HttpServletRequest;
import junit.framework.TestCase;
//other imports omitted
public class NameServletTest extends TestCase
{
public void testWithMockObjects() throws Exception
{
// strict mock forces you to specify the correct order of method calls
HttpServletRequest request = createStrictMock(HttpServletRequest.class);
expect(request.getParameter("firstName")).andReturn("laxman");
expect(request.getParameter("lastName")).andReturn("asman");
request.setAttribute("firstName", "laxman");
request.setAttribute("lastName", "asman");
//unexpected method calls after this point will throw exceptions
replay(request);
new NameServlet().doPost(request, null);
//check that the behaviour expected is actually
verify(request);
}
//more test methods omitted ...
}
No comments:
Post a Comment