Hi Justin, Thanks for your reply. But I'm still not seeing how to go about solving this.
Where I get stuck is how to get the HtmlRenderer to use my writer. It has a private PrintWriter field inside it, which as best I can tell is never actually initialized. If I just create a new HtmlRenderer() and then invoke one of the methods that makes use of the PrintWriter, I will get a NullPointerException because the PrintWriter is null. I think what you were suggesting is that I do it via an HttpServletResponse implementation that uses the HtmlRenderer. But I still don't have a good way to put my PrintWriter into the HtmlRenderer - not without some reflection hacks anyway. What am I missing here? On Wed, Feb 1, 2017 at 3:11 PM, Justin Edelson <[email protected]> wrote: > 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 <[email protected]> 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 > > >
