Created
November 23, 2012 09:06
-
-
Save egocks/4134650 to your computer and use it in GitHub Desktop.
Example of using Mockrunner to write JUnit tests for servlets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pointwest.example; | |
public class Employee { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pointwest.example; | |
public class EmployeeController { | |
public Object getEmployeeById(String id) { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pointwest.example; | |
import java.io.IOException; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
/** | |
* Servlet implementation class EmployeeServlet | |
*/ | |
public class EmployeeServlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
/** | |
* @see HttpServlet#HttpServlet() | |
*/ | |
public EmployeeServlet() { | |
super(); | |
// TODO Auto-generated constructor stub | |
} | |
/** | |
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) | |
*/ | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
// TODO Auto-generated method stub | |
if(null == request.getParameter("employeeId")) | |
response.setStatus(500); | |
} | |
/** | |
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) | |
*/ | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
// TODO Auto-generated method stub | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pointwest.example; | |
import junit.framework.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import com.mockrunner.mock.web.WebMockObjectFactory; | |
import com.mockrunner.servlet.ServletTestModule; | |
public class EmployeeServletTest { | |
private ServletTestModule tester; | |
private WebMockObjectFactory factory; | |
@Before | |
public void setup() { | |
factory = new WebMockObjectFactory(); | |
tester = new ServletTestModule(factory); | |
} | |
@Test | |
public void doGetNullID() { | |
String id = null; | |
int expectedCode = 500; | |
tester.addRequestParameter("employeeId", id); | |
// instantiate the servlet | |
tester.createServlet(EmployeeServlet.class); | |
// call doGet | |
tester.doGet(); | |
// assertion: status code should be 500 | |
Assert.assertEquals(expectedCode, factory.getMockResponse().getStatusCode()); | |
} | |
@Test | |
public void doGetValidID() { | |
String id = "eliel"; | |
int expectedCode = 200; | |
tester.addRequestParameter("employeeId", id); | |
// instantiate the servlet | |
tester.createServlet(EmployeeServlet.class); | |
// call doGet | |
tester.doGet(); | |
// assertion: status code should be 200 | |
Assert.assertEquals(expectedCode, factory.getMockResponse().getStatusCode()); | |
} | |
@Test | |
public void doGetSendsValidIDToController() { | |
String id = "eliel"; | |
int expectedCode = 200; | |
Employee empDetails = new Employee(); | |
// note: need to import PowerMock for this to work | |
EmployeeController mockController = createMock(EmployeeController.class); | |
expect(mockController.getEmployeeById(id)).andReturn(empDetails); | |
replay(EmployeeController.class, mockController); | |
tester.addRequestParameter("employeeId", id); | |
// instantiate the servlet | |
tester.createServlet(EmployeeServlet.class); | |
// call doGet | |
tester.doGet(); | |
verify(EmployeeController.class, mockController); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment