VelocityRenderer implements Renderer { @Override public void render(String path, Request request) throws IOException { if (path == null) { throw new CannotRenderException("Cannot dispatch a null path"); } ServletRequest servletRequest = ServletUtil.getServletRequest(request); Context context = velocityView.createContext(servletRequest .getRequest(), servletRequest.getResponse()); Template template = velocityView.getTemplate((String) path); velocityView.merge(template, context, request.getWriter()); } VelocityRenderer(VelocityView velocityView); @Override void render(String path, Request request); boolean isRenderable(String path, Request request);  }
@Test public void testRender() throws IOException { VelocityView view = createMock(VelocityView.class); ServletRequest request = createMock(ServletRequest.class); HttpServletRequest httpRequest = createMock(HttpServletRequest.class); HttpServletResponse response = createMock(HttpServletResponse.class); ViewToolContext context = createMock(ViewToolContext.class); Template template = createMock(Template.class); Writer writer = createMock(Writer.class); expect(request.getRequest()).andReturn(httpRequest); expect(request.getResponse()).andReturn(response); expect(view.createContext(httpRequest, response)).andReturn(context); expect(view.getTemplate("/test.vm")).andReturn(template); expect(request.getWriter()).andReturn(writer); view.merge(template, context, writer); replay(view, request, httpRequest, response, context, template, writer); Renderer renderer = new VelocityRenderer(view); renderer.render("/test.vm", request); verify(view, request, httpRequest, response, context, template, writer); }
@Test(expected = CannotRenderException.class) public void testRenderException() throws IOException { VelocityView view = createMock(VelocityView.class); ServletRequest request = createMock(ServletRequest.class); replay(view, request); Renderer renderer = new VelocityRenderer(view); try { renderer.render(null, request); } finally { verify(view, request); } }