Created
July 7, 2014 18:06
-
-
Save gissuebot/48b26f8b69194fb9b9e8 to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 251, comment 1
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 junit.framework.Assert.assertEquals; | |
import org.junit.Test; | |
import com.google.inject.AbstractModule; | |
import com.google.inject.Guice; | |
import com.google.inject.Injector; | |
import com.google.inject.ProvidedBy; | |
import com.google.inject.Provider; | |
import com.google.inject.Scopes; | |
import com.google.inject.Singleton; | |
public class ProvidedByScopeTest | |
{ | |
static class Class1Provider implements Provider<Class1> | |
{ | |
public Class1 get() | |
{ | |
return new Class1(); | |
} | |
} | |
@Singleton | |
@ProvidedBy( Class1Provider.class ) | |
static class Class1 | |
{ | |
@Override | |
public boolean equals( Object obj ) | |
{ | |
return this == obj; | |
} | |
} | |
@Test | |
public void testProvidedByWithScope() throws Exception | |
{ | |
final Injector i = Guice.createInjector(); | |
final Class1 i1 = i.getInstance( Class1.class ); | |
final Class1 i2 = i.getInstance( Class1.class ); | |
assertEquals( i1, i2 ); | |
} | |
@Test | |
public void testProvidedByWithScopeFromModule() throws Exception | |
{ | |
final Injector i = Guice.createInjector( new AbstractModule() { | |
@Override | |
protected void configure() | |
{ | |
bind( Class1.class ).in( Scopes.SINGLETON ); | |
} | |
} ); | |
final Class1 i1 = i.getInstance( Class1.class ); | |
final Class1 i2 = i.getInstance( Class1.class ); | |
assertEquals( i1, i2 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment