99
1010import java .io .ByteArrayOutputStream ;
1111import java .io .IOException ;
12+ import java .io .OutputStream ;
13+ import java .io .PrintStream ;
1214
1315import static org .junit .Assert .*;
16+ import static org .mockito .Mockito .*;
1417
1518public class ResponseWriterTest {
1619 private final ResponseWriter responseWriter ;
1720 private final ByteArrayOutputStream outputStream ;
21+ private final Logger logger ;
1822
1923 public ResponseWriterTest () {
20- this .outputStream = new ByteArrayOutputStream ();
21- this .responseWriter = new ResponseWriter (outputStream );
24+ outputStream = new ByteArrayOutputStream ();
25+ logger = mock (Logger .class );
26+ responseWriter = new ResponseWriter (outputStream , logger );
2227 }
2328
2429 @ Test
@@ -44,6 +49,18 @@ public void writesResponseHeaders() throws Exception {
4449 assertTrue (output .contains ("Content-Length: 7" ));
4550 }
4651
52+ @ Test
53+ public void logsExceptionIfWritingThrowsException () throws Exception {
54+ OutputStream outputStreamMock = mock (OutputStream .class );
55+ doThrow (new IOException ()).when (outputStreamMock ).write (any ());
56+ ResponseWriter responseWriter = new ResponseWriter (outputStreamMock , logger );
57+ Response response = new OkResponse ("example" .getBytes ());
58+
59+ responseWriter .write (response );
60+
61+ verify (logger ).log (any ());
62+ }
63+
4764 @ Test
4865 public void itWritesTheFirstLineFor404 () throws Exception {
4966 String output = outputForResponse (new NotFoundResponse ());
0 commit comments