Created
October 18, 2015 17:54
-
-
Save jangalinski/1bdc0cf1ec98ec9a6c64 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 com.github.jangalinski; | |
import com.google.inject.AbstractModule; | |
import com.google.inject.Guice; | |
import com.google.inject.Injector; | |
import org.junit.Test; | |
import javax.inject.Provider; | |
import javax.inject.Singleton; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class GuiceSingletonProviderTest { | |
@Test | |
public void execute() { | |
Injector injector = Guice.createInjector(new AbstractModule() { | |
@Override | |
protected void configure() { | |
bind(GlobalSettings.class).toProvider(GlobalSettingsProvider.class).in(Singleton.class); | |
} | |
}); | |
GlobalSettings g1 = injector.getInstance(GlobalSettings.class); | |
GlobalSettings g2 = injector.getInstance(GlobalSettings.class); | |
assertThat(g1).isSameAs(g2); | |
} | |
public static class GlobalSettingsProvider implements Provider<GlobalSettings> { | |
@Override | |
public GlobalSettings get() { | |
GlobalSettings globalSettings = new GlobalSettings(); | |
globalSettings.isDummyRun = Boolean.parseBoolean(System.getProperty("isDummyRun", "false")); | |
globalSettings.inputFlavor = System.getProperty("input_flavor", "none"); | |
return globalSettings; | |
} | |
} | |
private static class GlobalSettings { | |
public boolean isDummyRun; | |
public String inputFlavor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment