ServletPrintWriter extends PrintWriter { public void writeEnd() { if (wasWriteEndAlreadyCalled) { throw new IllegalStateException("This method can be called once only."); } wasWriteEndAlreadyCalled = true; } ServletPrintWriter(final OutputStream outputStream); void writeEnd();  }
@Test(expected = IllegalStateException.class) public void shouldNotAllowToWriteEndMoreThanOnce() { ServletPrintWriter servletPrintWriter = new ServletPrintWriter(mock(ServletOutputStream.class)); try { servletPrintWriter.writeEnd(); } catch (Throwable e) { fail("Should not throw any exception here"); } servletPrintWriter.writeEnd(); }