@Override public void installModules(Module... modules) { installModules(false, modules); }
@Override public synchronized void installTestModules(Module... modules) { if (hasTestModules) { throw new IllegalStateException("TestModules can only be installed once per scope."); } installModules(true, modules); hasTestModules = true; }
@Test(expected = IllegalStateException.class) public void installModules_shouldFailWhenModuleHasIssues() { //GIVEN ScopeImpl scope = new ScopeImpl(""); //WHEN scope.installModules(new BuggyModule()); //THEN fail(); }
@Test(expected = IllegalStateException.class) public void installModules_shouldThrowAnException_whenModuleHasANullBinding() { //GIVEN ScopeImpl scope = new ScopeImpl(""); //WHEN scope.installModules(new NullBindingModule()); //THEN fail("Should throw an exception."); }
@Test public void installOverrideModules_shouldNotInstallOverrideBindings_whenCalledWithoutTestModules() { //GIVEN ScopeImpl scope = new ScopeImpl(""); scope.installTestModules(); scope.installModules(new ProdModule()); //WHEN Foo instance = scope.getInstance(Foo.class); //THEN assertThat(instance, notNullValue()); }
@Override public void installModules(Module... modules) { installModules(false, modules); }
@Override public synchronized void installTestModules(Module... modules) { if (hasTestModules) { throw new IllegalStateException("TestModules can only be installed once per scope."); } installModules(true, modules); hasTestModules = true; }
@Override public synchronized void installTestModules(Module... modules) { if (hasTestModules) { throw new IllegalStateException("TestModules can only be installed once per scope."); } hasTestModules = false; installModules(modules); hasTestModules = true; }
@Test public void testToString() { //GIVEN ScopeImpl scope = new ScopeImpl("root"); scope.installModules(new TestModule1()); ScopeImpl childScope = new ScopeImpl("child"); scope.addChild(childScope); //WHEN childScope.getInstance(Bar.class); String dump = scope.toString(); //THEN Pattern expected = Pattern.compile("root:\\d+.*" + "Providers: \\[toothpick.Scope,toothpick.data.Foo\\].*" + "\\\\---child:\\d+.*" + "Providers:.*\\[toothpick.Scope\\].*" + "Unbound providers: \\[toothpick.data.Bar\\].*", Pattern.DOTALL); assertThat(dump, MatchesPattern.matchesPattern(expected)); }
@Test public void installOverrideModules_shouldNotOverrideOtherBindings() { //GIVEN Foo testFoo = new Foo(); ScopeImpl scope = new ScopeImpl(""); scope.installTestModules(new TestModule(testFoo)); scope.installModules(new ProdModule2()); //WHEN Foo fooInstance = scope.getInstance(Foo.class); Bar barInstance = scope.getInstance(Bar.class); //THEN assertThat(fooInstance, sameInstance(testFoo)); assertThat(barInstance, notNullValue()); }
@Test public void installTestModules_shouldOverrideBindings_whenInstalledAfterProductionModules() { //GIVEN Foo testFoo = new Foo(); ScopeImpl scope = new ScopeImpl(""); scope.installModules(new ProdModule()); scope.installTestModules(new TestModule(testFoo)); //WHEN Foo instance = scope.getInstance(Foo.class); //THEN assertThat(instance, sameInstance(testFoo)); }
@Test public void installOverrideModules_shouldInstallOverrideBindings_whenCalledOnce() { //GIVEN Foo testFoo = new Foo(); ScopeImpl scope = new ScopeImpl(""); scope.installTestModules(new TestModule(testFoo)); scope.installModules(new ProdModule()); //WHEN Foo instance = scope.getInstance(Foo.class); //THEN assertThat(instance, sameInstance(testFoo)); }