Last active
December 13, 2015 16:58
-
-
Save jangalinski/4943871 to your computer and use it in GitHub Desktop.
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
package sof14839561; | |
import static com.google.inject.Guice.createInjector; | |
import static com.google.inject.name.Names.named; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.junit.Assert.assertThat; | |
import javax.inject.Inject; | |
import javax.inject.Named; | |
import org.junit.Test; | |
import com.google.inject.AbstractModule; | |
import com.google.inject.Provider; | |
public class NamedProviderTest extends AbstractModule { | |
public static interface Foo { | |
String foo(); | |
} | |
public static class FooProvider implements Provider<Foo> { | |
@Override | |
public Foo get() { | |
return new Foo() { | |
@Override | |
public String foo() { | |
return "foo"; | |
} | |
}; | |
} | |
} | |
public static class FooPrimeProvider implements Provider<Foo> { | |
@Override | |
public Foo get() { | |
return new Foo() { | |
@Override | |
public String foo() { | |
return "prime"; | |
} | |
}; | |
} | |
} | |
@Inject | |
public Provider<Foo> fooProvider; | |
@Inject | |
@Named("prime") | |
public Provider<Foo> fooPrimeProvider; | |
@Test | |
public void shouldInjectFooAndPrime() { | |
createInjector(this).injectMembers(this); | |
assertThat(fooPrimeProvider.get().foo(), is("prime")); | |
assertThat(fooProvider.get().foo(), is("foo")); | |
} | |
@Override | |
protected void configure() { | |
bind(Foo.class).toProvider(FooProvider.class); | |
bind(Foo.class).annotatedWith(named("prime")).toProvider(FooPrimeProvider.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment