IdRegistry { @Nonnull public T get(String id) throws IllegalArgumentException { T t = map.get(id); if (t == null) { throw new IllegalArgumentException("Unknown id <" + id + ">"); } return t; } protected  IdRegistry(Set<T> instances); @Nonnull T get(String id);  }
@Test public void testNoImplsBoundDoesntCrash() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(FooRegistry.class); Multibinder.newSetBinder(binder(), Foo.class); } }); FooRegistry registry = injector.getInstance(FooRegistry.class); try { registry.get("foo"); fail(); } catch (IllegalArgumentException e) { assertEquals("Unknown id <foo>", e.getMessage()); } }
@Test public void testWorks() { Foo shouldWork = Guice.createInjector(Stage.PRODUCTION, new AbstractModule() { @Override protected void configure() { binder().requireExplicitBindings(); bind(FooRegistry.class); Multibinder.newSetBinder(binder(), Foo.class).addBinding().to(ShouldWork.class); } }).getInstance(FooRegistry.class).get("shouldWork"); assertNotNull(shouldWork); assertEquals((ShouldWork.class), shouldWork.getClass()); }