StartupStatus { public void startup(String message, Object... params) { notNull(message, "Invalid message '" + message + "' specified"); notNull(params, "Invalid params specified"); String formattedMessage = String.format(message, params); StartupStatusListener listener = StartupStatusListenerRegistry.getStartupListener(); if (listener != null) { listener.setStatus(formattedMessage); } logger.accept(formattedMessage); } StartupStatus(); private  StartupStatus(Consumer<String> logger); void startup(String message, Object... params);  }
@Test public void startup_nullStringId_throwsIllegalArgumentException() { String stringId = null; Object[] params = new Object[0]; StartupStatus startupStatus = new StartupStatus(); Throwable thrown = catchThrowable(() -> startupStatus.startup(stringId, params)); assertThat(thrown) .isInstanceOf(NullPointerException.class) .hasMessage("Invalid message 'null' specified"); }
@Test public void startup_emptyParams() { String stringId = "my string"; Object[] params = new Object[0]; StartupStatus startupStatus = new StartupStatus(); startupStatus.startup(stringId, params); assertThat(getStartupListener()) .isNull(); }
@Test public void startup_doesNothingIfNoListener() { String stringId = "my string"; Object[] params = new Object[0]; StartupStatus startupStatus = new StartupStatus(); startupStatus.startup(stringId, params); assertThat(getStartupListener()) .isNull(); }
@Test public void startup_invokesListener() { listener = statusMessage -> statusMessageList.add(statusMessage); String stringId = "my string"; Object[] params = new Object[0]; setListener(listener); StartupStatus startupStatus = new StartupStatus(); startupStatus.startup(stringId, params); assertThat(statusMessageList) .hasSize(1) .contains("my string"); }
@Test public void startupTwice_invokesListenerTwice() { listener = statusMessage -> statusMessageList.add(statusMessage); String stringIdOne = "my string"; String stringIdTwo = "other string"; Object[] params = new Object[0]; setListener(listener); StartupStatus startupStatus = new StartupStatus(); startupStatus.startup(stringIdOne, params); startupStatus.startup(stringIdTwo, params); assertThat(statusMessageList) .hasSize(2) .contains("my string") .contains("other string"); }