@Before public void setUpForDevelopmentConfiguration() throws Exception { Toothpick.setConfiguration(Configuration.forDevelopment()); } }
@BeforeClass public static void setUp() throws Exception { ToothpickBaseTest.setUp(); Toothpick.setConfiguration(Configuration.forDevelopment().disableReflection()); }
@Test(expected = MultipleRootException.class) public void opening2rootScope_shouldThrowAnException_whenConfigurationPreventsMultipleRootScopes() throws Exception { //GIVEN Toothpick.setConfiguration(Configuration.forDevelopment().preventMultipleRootScopes()); Toothpick.openScope("foo"); //WHEN Toothpick.openScope("bar"); //THEN }
@Test public void binding_shouldCreateAnnotatedClassInScopeBoundToScopeAnnotationViaFactory_whenParentScopeIsBoundToScopeAnnotation() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope scopeParent = Toothpick.openScope(CustomScope.class); Scope scope1 = Toothpick.openScopes(CustomScope.class, "child"); //WHEN FooCustomScope instanceInParent = scopeParent.getInstance(FooCustomScope.class); FooCustomScope instanceInChild = scope1.getInstance(FooCustomScope.class); //THEN assertThat(instanceInParent, sameInstance(instanceInChild)); } }
@Test(expected = MultipleRootException.class) public void openingAClosedChildScope_shouldThrowAnException_whenConfigurationPreventsMultipleRootScopes() throws Exception { //GIVEN Toothpick.setConfiguration(Configuration.forDevelopment().preventMultipleRootScopes()); Toothpick.openScopes("foo", "bar"); Toothpick.closeScope("bar"); //WHEN Toothpick.openScope("bar"); //THEN }
@Test public void binding_shouldCreateAnnotatedClassInScopeBoundToScopeAnnotationViaProvider_whenParentScopeIsBoundToScopeAnnotation() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope scopeParent = Toothpick.openScope(CustomScope.class); Scope scope1 = Toothpick.openScopes(CustomScope.class, "child"); //WHEN FooProviderAnnotatedProvidesSingleton instanceInParent = scopeParent.getInstance(FooProviderAnnotatedProvidesSingleton.class); FooProviderAnnotatedProvidesSingleton instanceInChild = scope1.getInstance(FooProviderAnnotatedProvidesSingleton.class); //THEN assertThat(instanceInParent, sameInstance(instanceInChild)); }
@Test public void binding_shouldCreateAnnotatedClassInRootScope_whenInjectingSingletonAnnotatedClass() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope scopeParent = Toothpick.openScope("root"); Scope scope1 = Toothpick.openScopes("root", "scope1"); //WHEN FooProviderAnnotatedSingletonImpl instanceInParent = scopeParent.getInstance(FooProviderAnnotatedSingletonImpl.class); FooProviderAnnotatedSingletonImpl instanceInChild = scope1.getInstance(FooProviderAnnotatedSingletonImpl.class); //THEN assertThat(instanceInParent, sameInstance(instanceInChild)); }
@Test public void singleton_shouldBeShared_whenBoundingIsSimpleOnSingletonAnnotatedClass() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope root = Toothpick.openScopes("root"); root.installModules(new Module() { { bind(FooSingleton.class); } }); //WHEN FooSingleton instance = root.getInstance(FooSingleton.class); FooSingleton instance2 = root.getInstance(FooSingleton.class); //THEN assertThat(instance, sameInstance(instance2)); }
@Test(expected = IllegalStateException.class) public void binding_shouldCrashForScopeAnnotatedClass_whenBindingIsSimple() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope scope1 = Toothpick.openScopes("root", "scope1"); //WHEN scope1.installModules(new Module() { { bind(FooSingleton.class); } }); //THEN fail("This test should throw a IllegalBindingException."); }
@Test public void singleton_shouldBeShared_whenBoundExplicitlyToSingletonAnnotatedClass() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope root = Toothpick.openScopes("root"); root.installModules(new Module() { { bind(IFooSingleton.class).to(FooSingleton.class); } }); //WHEN IFooSingleton instance = root.getInstance(IFooSingleton.class); IFooSingleton instance2 = root.getInstance(IFooSingleton.class); //THEN assertThat(instance, sameInstance(instance2)); }
@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)); }
@Test(expected = IllegalStateException.class) public void binding_shouldCrashForScopeAnnotatedClass_whenBindingToAProvider() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope scope1 = Toothpick.openScopes("root", "scope1"); //WHEN scope1.installModules(new Module() { { bind(IFoo.class).toProvider(FooProviderAnnotatedSingletonImpl.class); } }); //THEN fail("This test should throw a IllegalBindingException."); }
@Test(expected = IllegalStateException.class) public void binding_shouldCrashForScopeAnnotatedClass_whenBindingToAClass() throws Exception { //GIVEN Toothpick.setConfiguration(forDevelopment()); Scope scope1 = Toothpick.openScopes("root", "scope1"); //WHEN scope1.installModules(new Module() { { bind(IFooSingleton.class).to(FooSingleton.class); } }); //THEN fail("This test should throw a IllegalBindingException."); }