如何使用junit和jmock对MVC Model 2应用程序进行单元测试?
我有一个MVC Model 2应用程序功能齐全但没有单元测试。我正在围绕应用程序实现单元测试。我想单元测试我的控制器servlet方法。控制器servlet调用dao类来执行CRUD操作。示例:向用户显示登录对话框,用户输入userid / pwd并单击“提交”按钮。该请求由控制器的processRequest()和validateLogin()方法处理,然后这些方法通过DAO类查询数据库,最后如果登录成功,则返回带有数据的LoginBean。
我的问题是如何对这种代码进行单元测试?
为了更好地理解问题,下面提供了示例代码。
---- ControllerServlet.java ----
public class ControllerServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
public void processRequest(HttpServletRequest request, HttpServletResponse response, String opcode) throws ServletException, IOException {
String url = "index.jsp"; //default url
HttpSession session = request.getSession();
try {
if (opcode.equalsIgnoreCase("authenticate")) {
String firstName = GenericUtils.getParameter(request, "firstName");
String lastName = GenericUtils.getParameter(request, "lastName");
List userLst = validateLogin(firstName, lastName, request);
boolean loginValid = CollectionUtils.isNotEmpty(userLst);
request.setAttribute("loginValid", String.valueOf(loginValid)); //setting value in request attribute
//if login is valid set the LoginBean in session
if (loginValid) {
LoginBean loginBean = (LoginBean) userLst.get(0);
session.setAttribute("loginBean", loginBean);
getAllProducts(request);
url = "listProducts.jsp";
} else {
url = "login.jsp";
}
}
}
catch.....
//some more code here
}
public List validateLogin(String firstName, String lastName, HttpServletRequest request) throws ServletException, IOException {
List userLst = new ArrayList();
if (firstName.length() > 0 && lastName.length() > 0) {
userLst = MasterDao.checkLoginValid(firstName, lastName);//DAO call goes here
}
return userLst;
}
}
我如何对这种东西进行单元测试?我尝试使用junit和jmock创建单元测试,但我不确定如何使用jmock传递请求参数,第二个问题是我在服务器启动时使用JNDI创建数据库连接,因此我的单元测试总是会失败的地方DAO调用已完成,因为单元测试在编译时执行。如何解决这两个问题并对此功能进行单元测试?
答案 0 :(得分:1)
我使用Spring Web模拟框架 http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/mock/web/package-summary.html
import org.agitar.mock.servlet.MockHttpServletRequest;
import org.agitar.mock.servlet.MockHttpServletResponse;
import org.junit.Before;
import org.springframework.mock.web.MockServletConfig;
public class ControllerServletTest {
private ControllerServlet servlet;
private MockServletConfig config;
private MockHttpServletRequest request;
private MockHttpServletResponse response;
@Before
public void setUp()
throws Exception {
servlet = new ControllerServlet();
config = new MockServletConfig();
servlet.init(config);
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}
@Test
public void authenticate() throws Exception {
request.putParameter("firstName", "Joe");
request.putParameter("lastName", "Schmoe");
// etc ...
servlet.processRequest(request, response, "authenticate");
// etc ...
}
}