### Question:
ComponentDiscovery { public List<ComponentRegistrar> discover() { return instantiate(retriever.retrieve(context)); } @VisibleForTesting  ComponentDiscovery(T context, RegistrarNameRetriever<T> retriever); static ComponentDiscovery<Context> forContext(
      Context context, Class<? extends Service> discoveryService); List<ComponentRegistrar> discover();  }### Answer:
@Test public void discover_shouldCorrectlyInstantiateValidComponentRegistrars() { when(retriever.retrieve(context)) .thenReturn(Arrays.asList(TestRegistrar1.class.getName(), TestRegistrar2.class.getName())); List<ComponentRegistrar> result = discovery.discover(); assertThat(result).hasSize(2); ComponentRegistrar registrar; registrar = result.get(0); assertThat(registrar).isInstanceOf(TestRegistrar1.class); registrar = result.get(1); assertThat(registrar).isInstanceOf(TestRegistrar2.class); }
@Test public void discover_shouldSkipClassesThatDontImplementComponentRegistrarInterface() { when(retriever.retrieve(context)) .thenReturn(Collections.singletonList(InvalidRegistrar.class.getName())); assertThat(discovery.discover()).isEmpty(); }
@Test public void discover_shouldSkipNonExistentClasses() { when(retriever.retrieve(context)) .thenReturn(Collections.singletonList("com.example.DoesNotExistClass")); assertThat(discovery.discover()).isEmpty(); }
@Test public void discover_shouldSkipPrivateClasses() { when(retriever.retrieve(context)) .thenReturn(Collections.singletonList(PrivateTestRegistrar.class.getName())); assertThat(discovery.discover()).isEmpty(); }
@Test public void discover_shouldSkipClassesWithNoDefaultConstructors() { when(retriever.retrieve(context)) .thenReturn(Collections.singletonList(NoDefaultCtorRegistrar.class.getName())); assertThat(discovery.discover()).isEmpty(); }