spring的配置文件此处省略

  单元测试:

public class CaseControllerTest {
 private static final Log logger = LogFactory.getLog(CaseControllerTest.class);
 private CaseController controller = null;
 private XmlWebApplicationContext ctx;

 
 @Before
 public void setUp() throws Exception {
  String[] paths = {"spring-servlet-test.xml" };
  ctx = new XmlWebApplicationContext();
  ctx.setConfigLocations(paths);
  ctx.setServletContext(new MockServletContext(""));
  ctx.refresh();
  controller = (CaseController) ctx.getBean("caseController");
 }

 
 @After
 public void tearDown() throws Exception {
  ctx.close();
 }

 
 @Test
 public void testListJson() {
  logger.info("*****testListJson start**********");
  MockHttpServletResponse response = new MockHttpServletResponse();
  MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  request.setRequestURI("/case/listJson");
  request.addParameter("name", "name");
  List<String> result = controller.listJson(request, response);
  logger.info("*****result:" + result);
  Assert.assertTrue("result is empty", !result.isEmpty());

  logger.info("*****testListJson end**********");
 }
 
 @Test
 public void testList() {
  logger.info("*****testList start**********");
  MockHttpServletResponse response = new MockHttpServletResponse();
  MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  request.setRequestURI("/case/list");
  request.addParameter("name", "name");
  String returnStr = controller.list(request, response);
  List<String> result = (List<String>) request.getAttribute("result");
  logger.info("*****result:" + result);
  logger.info("*****returnStr:" + returnStr);
  logger.info("*****testList end**********");
 }
}

  注意点:

1.spring-servlet-test.xml配置文件要对;
2.controller的bean首字母是小写即caseController;
3.可以通过request response直接获取相应的数据,如request.getAttribute("result");