2 yjjhebe yjjhebe 于 2015.06.16 16:08 提问

sprinmvc 怎么进行单元测试?

项目springmvc + mybatis,用maven搭的框架,现在要对controller层进行单元测试,
请问怎么测,给下思路?其他方法都是用junit来测,现在controller是不是要模拟HTTP请求。
研究了下mokito等测试框架,弄不出来。谢谢!

1个回答

a415613911
a415613911   2015.06.16 17:30

mock主要是帮助你模拟一些不容易被构建的函数、类或者接口。我这有个简单的小例子,你可以参考。我用easymock测试 ,官网:http://www.easymock.org/
被测试类:
package controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import po.User;
import service.UserService;

@Controller
public class UserController{

@Autowired
private UserService userService;

public void setUserService(UserService userService) {
    this.userService = userService;
}

@RequestMapping(value="getUser")
public void handleRequest(HttpServletRequest request,HttpServletResponse response) throws IOException{
    String id = request.getParameter("id");
    if(StringUtils.isBlank(id)){
        response.getWriter().write("1");
        return;
    }

    User user = null;
    try{
        user = userService.getUserById(Long.valueOf(id));
    }catch(RuntimeException e){

    }

    if(user == null){
        response.getWriter().write("2");
        return;
    }

    response.getWriter().write(user.getName());
}

}

测试代码:
package controller;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.easymock.EasyMock.*;

import org.easymock.IMocksControl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import po.User;
import service.UserService;

public class UserControllerTest {
private HttpServletRequest request;
private HttpServletResponse response;
private PrintWriter pw;
private UserController uc;
private IMocksControl ic;
private UserService userService;

@Before
public void setUp(){
    uc = new UserController();
    ic = createControl(); 
    request = ic.createMock(HttpServletRequest.class);
    response = ic.createMock(HttpServletResponse.class);
    pw = ic.createMock(PrintWriter.class);
    userService = ic.createMock(UserService.class);
    uc.setUserService(userService);
}

@After
public void tearDown(){
    ic.verify();
}


@Test
public void testIdIsNull() throws IOException {
    expect(request.getParameter("id")).andReturn(null);
    expect(response.getWriter()).andReturn(pw);
    pw.write("1");
    expectLastCall().once();
    ic.replay();;
    uc.handleRequest(request, response);
}

@Test
public void testThrowException() throws IOException {
    expect(request.getParameter("id")).andReturn("1");
    expect(userService.getUserById(1)).andThrow(new RuntimeException());
    expect(response.getWriter()).andReturn(pw);
    pw.write("2");
    expectLastCall().once();
    ic.replay();;
    uc.handleRequest(request, response);
}

@Test
public void test() throws IOException {
    expect(request.getParameter("id")).andReturn("1");
    User user = new User(1, "fuhaiq");
    expect(userService.getUserById(1)).andReturn(user);
    expect(response.getWriter()).andReturn(pw);
    pw.write(user.getName());
    expectLastCall().once();
    ic.replay();;
    uc.handleRequest(request, response);
}

}

Csdn user default icon
上传中...
上传图片
插入图片