如何给Spring3 MVC中的Action做JUnit单元测试

 我来答
匿名用户
2015-09-10
展开全部
1. 测试一般的类
写一个抽象类,所有的测试类都继承它[java] @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath*:application-context-test.xml" }) public abstract class AbstractJUnit { } [java] public class OriginAndDestinationServiceTestCase extends AbstractJUnit { @Autowired private IOriginAndDestinationService service; private OriginADestinationRequestDTO request; @Before public void init() { this.request = new OriginADestinationRequestDTO(); this.request.setBrand("KA"); this.request.setBookingFlow("REV"); this.request.setLocale("en_HK"); this.request.setOrigin("HKG"); this.request.setSector(0); this.request.setCorrelationInfo(new CorrelationInfo("aaaa", "bbbb", "cccc")); } @Test public void testService() { OriginADestinationResponseDTO response = this.service.invoke(this.request); Assert.assertNotNull(response); } } 2. 测试Controller, 这里使用的是Spring MVC框架,测试Action或是servlet也差不多 还是弄一个抽象类[java] @ContextConfiguration(locations = { "classpath*:application-context-junit.xml", "file:src/main/webapp/WEB-INF/spring3-servlet.xml" }) public class JUnitActionBase extends AbstractJUnit4SpringContextTests { /** * default http port. */ private static final int DEFAULT_PORT = 80; /** * DefaultAnnotationHandlerMapping. */ @Resource(type = DefaultAnnotationHandlerMapping.class) protected HandlerMapping handlerMapping; /** * AnnotationMethodHandlerAdapter. */ @Resource(type = AnnotationMethodHandlerAdapter.class) protected HandlerAdapter handlerAdapter; /** * Simulate Request to URL appoint by MockHttpServletRequest. * * @param request * HttpServletRequest * @param response * HttpServletResponse * @return ModelAndView * @throws Exception * runtimeException */ public final ModelAndView excuteAction(final HttpServletRequest request, final HttpServletResponse response) throws Exception { HandlerExecutionChain chain = this.handlerMapping.getHandler(request); final ModelAndView model = this.handlerAdapter.handle(request, response, chain.getHandler()); return model; } /** * Simulate Request to URL appoint by MockHttpServletRequest, default POST, port 80. * * @param url * requestURL * @param objects * parameters * @return ModelAndView */ public final ModelAndView excuteAction(final String url, final Object[]... objects) { return this.excuteAction("POST", url, JUnitActionBase.DEFAULT_PORT, objects); } /** * Simulate Request to URL appoint by MockHttpServletRequest, default POST. * * @param url * requestURL * @param port * int * @param objects * parameters * @return ModelAndView */ public final ModelAndView excuteAction(final String url, final int port, final Object[]... objects) { return this.excuteAction("POST", url, port, objects); } /** * Simulate Request to URL appoint by MockHttpServletRequest. * * @param method * POST/GET * @param url * requestURL * @param port * int * @param objects * parameters * @return ModelAndView */ public final ModelAndView excuteAction(final String method, final String url, final int port, final Object[]... objects) { MockHttpServletRequest request = new MockHttpServletRequest(method, url); MockHttpServletResponse response = new MockHttpServletResponse(); request.setServerPort(port); request.setLocalPort(port); if (objects != null) { for (Object[] object : objects) { if (object != null && object.length == 2) { request.addParameter(object[0].toString(), object[1].toString()); } } } MockHttpSession session = new MockHttpSession(); request.setSession(session); try { return this.excuteAction(request, response); } catch (Exception e) { e.printStackTrace(); InfoLogUtil.error(e.toString()); } return null; } } 测试类[java] public class LocationInfoTest extends JUnitActionBase { /** * TODO: write description for this method. */ @Test public void testKeepAlive() { Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("ACTION", "KEEP_ALIVE"); this.excuteAction("/IBEFacade", 8080, paramMap); } /** * TODO: write description for this method. */ @Test public void testLocationInfo() { this.excuteAction("/IBEFacade", 8080, new Object[]{"ACTION", "LOCATION_INFO"}); } } 另外还可以配合easymock/powermock框架来测试
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式