@Test(expected = IllegalArgumentException.class) public void testBindScopeAnnotation_shouldFail_whenAnnotationIsNotAScope() { //GIVEN Scope scope = Toothpick.openScope("root"); //WHEN scope.bindScopeAnnotation(NotAScope.class); //THEN fail("Should throw an exception"); }
@Test public void testBindScopeAnnotation_shouldReturnTrue_whenBound() { //GIVEN Scope parentScope = Toothpick.openScope("root"); parentScope.bindScopeAnnotation(CustomScope.class); //WHEN boolean isBoundToScopeAnnotation = parentScope.isBoundToScopeAnnotation(CustomScope.class); //THEN assertThat(isBoundToScopeAnnotation, is(true)); }
@Test public void testGetParentScope_shouldReturnItself_whenBoundToScopeAnnotation() { //GIVEN Scope childScope = Toothpick.openScopes("root", "child"); childScope.bindScopeAnnotation(CustomScope.class); //WHEN Scope scope = childScope.getParentScope(CustomScope.class); //THEN assertThat(scope, is(childScope)); }
@Test(expected = IllegalArgumentException.class) public void testBindScopeAnnotation_shouldFail_whenSingleton() { //GIVEN Scope scope = Toothpick.openScope("root"); //WHEN scope.bindScopeAnnotation(Singleton.class); //THEN fail("Should throw an exception"); }
@Test public void testGetParentScope_shouldReturnParentScope_whenParentBoundToScopeAnnotation() { //GIVEN Scope parentScope = Toothpick.openScope("root"); parentScope.bindScopeAnnotation(CustomScope.class); Scope childScope = Toothpick.openScopes("root", "child"); //WHEN Scope scope = childScope.getParentScope(CustomScope.class); //THEN assertThat(scope, is(parentScope)); }
@Test public void providerClassBinding_shouldCreateNonInjectedSingleton_whenProviderClassIsAnnotatedProvidesSingleton() { //GIVEN Scope scope = new ScopeImpl(""); scope.bindScopeAnnotation(CustomScope.class); scope.installModules(new Module() { { bind(IFoo.class).toProvider(FooProviderAnnotatedProvidesSingleton.class); } }); //WHEN IFoo foo = scope.getInstance(IFoo.class); IFoo foo2 = scope.getInstance(IFoo.class); //THEN assertThat(foo, notNullValue()); assertThat(foo2, sameInstance(foo)); }
@Test public void singleton_shouldBeShared_whenBoundingIsSimpleOnScopeAnnotatedClass() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope childScope = Toothpick.openScopes("root", "child"); childScope.bindScopeAnnotation(CustomScope.class); childScope.installModules(new Module() { { bind(FooProviderAnnotatedProvidesSingleton.class); } }); //WHEN FooProviderAnnotatedProvidesSingleton instance = childScope.getInstance(FooProviderAnnotatedProvidesSingleton.class); FooProviderAnnotatedProvidesSingleton instance2 = childScope.getInstance(FooProviderAnnotatedProvidesSingleton.class); //THEN assertThat(instance, sameInstance(instance2)); }