Hi Matt,
Thanks for adding tests!!!

What I would suggest is to mock the HttpServletResponse and provide a your
own StringWriter to response.getWriter().

Regards,
Justin

On Wed, Feb 1, 2017 at 4:26 PM Matt Ryan <o...@mvryan.org> wrote:

> Hi,
>
> I'm trying to add unit tests for
> org.apache.sling.junit.impl.servlet.HtmlRenderer.  However, I'm not sure
> the best way to verify that the correct behavior has occurred for the
> public methods.  Each public method takes action on a private field - a
> PrintWriter instance - but I'm not seeing any public way to inspect that
> field or get the output.
>
> One approach I know will work is to use reflection to initialize the
> PrintWriter and then to capture the output after calling a method.  This
> approach technically will work, but it makes the code brittle.
>
> Other options would be to make the PrintWriter protected, so I can inspect
> it from a child class, or add a method to the HtmlRenderer that returns the
> contents of the PrintWriter, e.g.
>
> public String render() {
>     output.flush();
>     return new String(outputStream.getBytes());
> }
>
> Any suggestions on the approach from the community?
>
>
> -MR
>

Reply via email to