BannerManager { public void outputBanner() { BannerPrinter bp = new SilentBannerPrinter(); switch (bannerTarget) { case CONSOLE: bp = new ConsoleBannerPrinter(); break; case LOGGER: bp = new LoggingBannerPrinter(); break; case OFF: default: break; } bp.outputBanner(); } BannerManager(String value); void outputBanner();  }
@Test public void valueConsoleSendsBannerToSystemOut() { SecurityManager sm = new SecurityManager(); AccessControlContext con = AccessController.getContext(); Permission p = new RuntimePermission("setIO"); sm.checkPermission(p, con); PrintStream standard = System.out; PrintStream printStream = Mockito.mock(PrintStream.class); System.setOut(printStream); standard.println("Changed stream for STDOUT successfully"); try { BannerManager bm = new BannerManager("console"); bm.outputBanner(); } finally { System.setOut(standard); } Mockito.verify(printStream, Mockito.atLeastOnce()).println(Mockito.anyString()); }
@Test public void valueLoggerSendsBannerToSystemOut() { SecurityManager sm = new SecurityManager(); AccessControlContext con = AccessController.getContext(); Permission p = new RuntimePermission("setIO"); sm.checkPermission(p, con); PrintStream standard = System.out; PrintStream printStream = Mockito.mock(PrintStream.class); System.setOut(printStream); standard.println("Changed stream for STDOUT successfully"); try { BannerManager bm = new BannerManager("LOGGER"); bm.outputBanner(); } finally { System.setOut(standard); } Mockito.verifyZeroInteractions(printStream); }
@Test public void invalidValueAvoidsLoggingToConsonle() { PrintStream standard = System.out; PrintStream printStream = Mockito.mock(PrintStream.class); System.setOut(printStream); try { BannerManager bm = new BannerManager("snafu"); bm.outputBanner(); } finally { System.setOut(standard); } Mockito.verify(printStream, Mockito.never()).println(Mockito.anyString()); }