Create a gist now

Instantly share code, notes, and snippets.

Guice AssistedInject
import org.junit.Test;
import static;
import static org.junit.Assert.assertThat;
public class GuiceExploratoryTest {
private static final String VALUE_ASSISTED_NAME = "value";
public void instantiateFactory() {
final Injector injector = Guice.createInjector(new Module());
final AoeuFactory aoeuFactory = injector.getInstance(AoeuFactory.class);
final Aoeu aoeu = aoeuFactory.create("aoeu");
assertThat(aoeu.getValueKnownByTheCreatorOfAoeuClass(), is("aoeu"));
public static class Module extends AbstractModule {
protected void configure() {
install(new FactoryModuleBuilder()
.implement(Aoeu.class, Aoeu.class)
public static class Aoeu {
private final DependencyInjectedTheUsualWay dependencyInjectedTheUsualWay;
private final String valueKnownByTheCreatorOfAoeuClass;
public Aoeu(
final DependencyInjectedTheUsualWay dependencyInjectedTheUsualWay,
@Assisted(VALUE_ASSISTED_NAME) final String valueKnownByTheCreatorOfAoeuClass) {
this.dependencyInjectedTheUsualWay = dependencyInjectedTheUsualWay;
this.valueKnownByTheCreatorOfAoeuClass = valueKnownByTheCreatorOfAoeuClass;
public String getValueKnownByTheCreatorOfAoeuClass() {
return valueKnownByTheCreatorOfAoeuClass;
public static interface AoeuFactory {
public Aoeu create(
@Assisted(VALUE_ASSISTED_NAME) final String string);
private static class DependencyInjectedTheUsualWay {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment