public void inject(Object objectUnderTest) { Toothpick.inject(objectUnderTest, scope); }
private SecondEntryPoint() { Scope scope = Toothpick.openScope("SecondEntryPoint"); Toothpick.inject(this, scope); } }
public SimpleEntryPoint() { Scope scope = Toothpick.openScope("SimpleEntryPoint"); Toothpick.inject(this, scope); }
@Test public void testSimpleInjection() throws Exception { //GIVEN Scope scope = new ScopeImpl(""); Foo foo = new Foo(); //WHEN Toothpick.inject(foo, scope); //THEN assertThat(foo.bar, notNullValue()); assertThat(foo.bar, isA(Bar.class)); }
@Test public void testInjection_shouldNotThrowAnException_whenNoDependencyIsFound() throws Exception { //GIVEN Scope scope = new ScopeImpl("root"); NotInjectable notInjectable = new NotInjectable(); //WHEN Toothpick.inject(notInjectable, scope); //THEN // nothing }
@Test public void testSimpleInjection() throws Exception { //GIVEN Scope scope = new ScopeImpl(""); FooWithLazy fooWithLazy = new FooWithLazy(); //WHEN Toothpick.inject(fooWithLazy, scope); //THEN assertThat(fooWithLazy.bar, notNullValue()); assertThat(fooWithLazy.bar, isA(Lazy.class)); Bar bar1 = fooWithLazy.bar.get(); assertThat(bar1, isA(Bar.class)); Bar bar2 = fooWithLazy.bar.get(); assertThat(bar2, isA(Bar.class)); assertThat(bar2, sameInstance(bar1)); }
@Test(expected = IllegalStateException.class) public void testLazyAfterClosingScope() throws Exception { //GIVEN String scopeName = ""; FooWithLazy fooWithLazy = new FooWithLazy(); //WHEN Toothpick.inject(fooWithLazy, Toothpick.openScope(scopeName)); Toothpick.closeScope(scopeName); System.gc(); //THEN fooWithLazy.bar.get(); // should crash } }
@Test public void testInjection_shouldWork_whenInheritingBinding() throws Exception { //GIVEN Scope scope = Toothpick.openScope("root"); scope.installModules(new Module() { { bind(Foo.class).to(Foo.class); } }); Scope childScope = Toothpick.openScopes("root", "child"); Foo foo = new Foo(); //WHEN Toothpick.inject(foo, childScope); //THEN assertThat(foo.bar, notNullValue()); assertThat(foo.bar, isA(Bar.class)); }
@Test public void testNamedInjection() throws Exception { //GIVEN Scope scope = new ScopeImpl(""); scope.installModules(new Module() { { bind(Bar.class).withName("foo").to(Bar.class); } }); FooWithNamedLazy fooWithLazy = new FooWithNamedLazy(); //WHEN Toothpick.inject(fooWithLazy, scope); //THEN assertThat(fooWithLazy.bar, notNullValue()); assertThat(fooWithLazy.bar, isA(Lazy.class)); Bar bar1 = fooWithLazy.bar.get(); assertThat(bar1, isA(Bar.class)); Bar bar2 = fooWithLazy.bar.get(); assertThat(bar2, isA(Bar.class)); assertThat(bar2, sameInstance(bar1)); }
@Override protected void onCreate(Bundle savedInstanceState) { scope = Toothpick.openScopes(getApplication(), PRESENTER_SCOPE, this); scope.installModules(new SmoothieActivityModule(this)); super.onCreate(savedInstanceState); Toothpick.inject(this, scope); setContentView(R.layout.simple_activity); ButterKnife.bind(this); title.setText("MVP"); subscription = rxPresenter.subscribe(aLong -> subTitle.setText(String.valueOf(aLong))); button.setVisibility(View.GONE); }
@Override protected void onCreate(Bundle savedInstanceState) { scope = Toothpick.openScopes(getApplication(), PRESENTER_SCOPE, this); scope.installModules(new SmoothieActivityModule(this)); super.onCreate(savedInstanceState); Toothpick.inject(this, scope); setContentView(R.layout.simple_activity); ButterKnife.bind(this); title.setText("MVP"); subTitle.setText(contextNamer.getInstanceCount()); button.setVisibility(View.GONE); }
@Override protected void onCreate(Bundle savedInstanceState) { scope = Toothpick.openScopes(getApplication(), this); scope.installModules(new SmoothieActivityModule(this)); super.onCreate(savedInstanceState); Toothpick.inject(this, scope); setContentView(R.layout.simple_activity); ButterKnife.bind(this); title.setText(contextNamer.getApplicationName()); subTitle.setText(contextNamer.getActivityName()); button.setText("click me !"); }
@Override protected void onCreate(Bundle savedInstanceState) { scope = Toothpick.openScopes(getApplication(), this); scope.installModules(new SmoothieActivityModule(this)); super.onCreate(savedInstanceState); Toothpick.inject(this, scope); setContentView(R.layout.simple_activity); ButterKnife.bind(this); title.setText(contextNamer.getApplicationName()); subTitle.setText(contextNamer.getActivityName()); }
new FeatureAnimatorModule(), new FeatureItemDecorationModule()); inject(this, scope); super.onCreate(savedInstanceState); setContentView(R.layout.activity_with_recycler);