NetworkPublisher implements AutoCloseable { public final void setName(String name) { checkArgument(!name.isEmpty(), "Name cannot be an empty string"); if (this.name.equals(Optional.of(name))) { return; } publishNameChanged(this.name, name); this.name = Optional.of(name); } protected  NetworkPublisher(); final void setName(String name); abstract void publish(T publish); @Override abstract void close();  }
@Test public void testThatNameChangeIsCalledInitiallyButNotAgainIfNameIsSame() { final String name = "QUACKERY!"; final int[] publishNameChangedCallCount = {0}; final TestNetworkPublisher publisher = new TestNetworkPublisher() { @Override protected void publishNameChanged(Optional<String> oldName, String newName) { publishNameChangedCallCount[0]++; assertEquals("Name was not the new name", name, newName); } }; publisher.setName(name); assertEquals("publishNameChanged was called an unexpected number of times", 1, publishNameChangedCallCount[0]); publisher.setName(name); assertEquals("publishNameChanged should not have been called agian", 1, publishNameChangedCallCount[0]); }