Created
July 7, 2014 19:14
-
-
Save gissuebot/ee7d6ce19118554f6531 to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 791, comment 0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertTrue; | |
import java.util.Set; | |
import com.google.inject.AbstractModule; | |
import com.google.inject.Guice; | |
import com.google.inject.Injector; | |
import com.google.inject.Key; | |
import com.google.inject.Singleton; | |
import com.google.inject.TypeLiteral; | |
import com.google.inject.multibindings.Multibinder; | |
import org.junit.Test; | |
public class GuiceTest { | |
private static interface A { | |
} | |
private static class B implements A { | |
} | |
@Singleton | |
private static class C implements A { | |
} | |
private final Injector inj = Guice.createInjector(new AbstractModule() { | |
@Override | |
protected void configure() { | |
Multibinder<A> binder = Multibinder.newSetBinder(binder(), A.class); | |
binder.addBinding().to(B.class).in(Singleton.class); | |
binder.addBinding().to(C.class); | |
} | |
}); | |
@Test | |
public void getInstance_ofB_IsIdentical() { | |
assertTrue(inj.getInstance(B.class) == inj.getInstance(B.class)); | |
} | |
@Test | |
public void getInstance_ofC_IsIdentical() { | |
assertTrue(inj.getInstance(C.class) == inj.getInstance(C.class)); | |
} | |
@Test | |
public void getInstance_ofSet_ContainsB() { | |
assertTrue(inj.getInstance(Key.get(new TypeLiteral<Set<A>>() { | |
})).contains(inj.getInstance(B.class))); | |
} | |
@Test | |
public void getInstance_ofSet_ContainsC() { | |
assertTrue(inj.getInstance(Key.get(new TypeLiteral<Set<A>>() { | |
})).contains(inj.getInstance(C.class))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment