44import org .junit .Test ;
55
66import java .io .IOException ;
7+ import java .io .PrintStream ;
78import java .nio .file .Path ;
89import java .nio .file .Paths ;
910import java .nio .file .StandardOpenOption ;
1314import static java .nio .file .Files .write ;
1415import static java .nio .file .Files .exists ;
1516import static org .junit .Assert .*;
17+ import static org .mockito .Mockito .*;
1618
1719public class LoggerTest {
1820
1921 private final Path logPath ;
2022 private final Logger logger ;
23+ private final PrintStream printStreamMock ;
2124
2225 public LoggerTest () throws IOException {
2326 logPath = Paths .get (tempDir ().toString (), "logs" );
24- logger = new Logger (logPath , new FileOperator ());
27+ printStreamMock = mock (PrintStream .class );
28+ logger = new Logger (logPath , new FileOperator (), printStreamMock );
2529 }
30+
2631 @ Test
2732 public void createsLogFileOnConstructDoesntOverwrite () throws Exception {
2833 assertTrue (exists (logPath ));
2934
3035 write (logPath , "fake log line\r \n " .getBytes (),
3136 StandardOpenOption .APPEND );
3237
33- new Logger (logPath , new FileOperator ());
34-
3538 assertEquals ("fake log line\r \n " , new String (readAllBytes (logPath )));
3639 }
3740
@@ -44,4 +47,15 @@ public void writesToLogAndReadsFromLog() throws Exception {
4447
4548 assertEquals ("POST example\r \n HEAD example\r \n " , log );
4649 }
50+
51+ @ Test
52+ public void printsErrorToStdoutIfCantWriteToLog () throws Exception {
53+ FileOperator fileOperatorMock = mock (FileOperator .class );
54+ doThrow (new IOException ()).when (fileOperatorMock ).appendToFile (any (), any ());
55+ Logger logger = new Logger (logPath , fileOperatorMock , printStreamMock );
56+
57+ logger .log ("" );
58+
59+ verify (printStreamMock ).print (any (Exception .class ));
60+ }
4761}
0 commit comments