### Question:
RestrictedComponentContainer extends AbstractComponentContainer { @Override public <T> T get(Class<T> anInterface) { if (!allowedDirectInterfaces.contains(anInterface)) { throw new IllegalArgumentException( String.format("Attempting to request an undeclared dependency %s.", anInterface)); } T value = delegateContainer.get(anInterface); if (!anInterface.equals(Publisher.class)) { return value; } @SuppressWarnings("unchecked") T publisher = (T) new RestrictedPublisher(allowedPublishedEvents, (Publisher) value); return publisher; } RestrictedComponentContainer(Component<?> component, ComponentContainer container); @Override T get(Class<T> anInterface); @Override Provider<T> getProvider(Class<T> anInterface); @Override Provider<Set<T>> setOfProvider(Class<T> anInterface); @Override Set<T> setOf(Class<T> anInterface);  }### Answer:
@Test public void get_withAllowedClass_shouldReturnAnInstanceOfThatClass() { Float value = 1.0f; when(delegate.get(Float.class)).thenReturn(value); assertThat(container.get(Float.class)).isSameInstanceAs(value); verify(delegate).get(Float.class); }
@Test public void get_withNotAllowedClass_shouldThrow() { try { container.get(List.class); fail("Expected exception not thrown."); } catch (IllegalArgumentException ex) { assertThat(ex.getMessage()).contains("java.util.List"); } }
@Test public void get_withProviderClass_shouldThrow() { try { container.get(Double.class); fail("Expected exception not thrown."); } catch (IllegalArgumentException ex) { assertThat(ex.getMessage()).contains("java.lang.Double"); } }
@Test public void get_withPublisher_shouldThrow() { try { container.get(Publisher.class); fail("Expected exception not thrown."); } catch (IllegalArgumentException ex) { assertThat(ex.getMessage()).contains("events.Publisher"); } }
@Test public void publish_withDeclaredEvent_shouldSucceed() { when(delegate.get(Publisher.class)).thenReturn(mockPublisher); Publisher publisher = publishingContainer.get(Publisher.class); Event<Float> event = new Event<>(Float.class, 1f); publisher.publish(event); verify(mockPublisher).publish(event); }
@Test public void publish_withUndeclaredEvent_shouldThrow() { when(delegate.get(Publisher.class)).thenReturn(mockPublisher); Publisher publisher = publishingContainer.get(Publisher.class); Event<Double> event = new Event<>(Double.class, 1d); try { publisher.publish(event); fail("Expected exception not thrown."); } catch (IllegalArgumentException ex) { assertThat(ex.getMessage()).contains("java.lang.Double"); } verify(mockPublisher, never()).publish(event); }