????Spring??mock????????е??????
???????????? ???????[ 2013/8/22 13:26:57 ] ????????
????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");
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11