toothpick.config.Binding.toProvider()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(6.4k)|赞(0)|评价(0)|浏览(125)

本文整理了Java中toothpick.config.Binding.toProvider()方法的一些代码示例,展示了Binding.toProvider()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Binding.toProvider()方法的具体详情如下:
包路径:toothpick.config.Binding
类名称:Binding
方法名:toProvider

Binding.toProvider介绍

暂无

代码示例

代码示例来源:origin: stephanenicolas/toothpick

  1. @Test
  2. public void providerClassBinding_shouldCreateInstancesViaProviderInstances_whenProviderClassIsNotAnnotated() {
  3. //GIVEN
  4. Scope scope = new ScopeImpl("");
  5. scope.installModules(new Module() {
  6. {
  7. bind(IFoo.class).toProvider(IFooProvider.class);
  8. }
  9. });
  10. //WHEN
  11. IFoo foo = scope.getInstance(IFoo.class);
  12. IFoo foo2 = scope.getInstance(IFoo.class);
  13. //THEN
  14. assertThat(foo, notNullValue());
  15. assertThat(foo2, not(sameInstance(foo)));
  16. assertThat(((Foo) foo).bar, notNullValue());
  17. assertThat(((Foo) foo2).bar, notNullValue());
  18. assertThat(((Foo) foo).bar, not(sameInstance(((Foo) foo2).bar)));
  19. }

代码示例来源:origin: stephanenicolas/toothpick

  1. @Test
  2. public void providerClassBinding_shouldCreateInjectedProviderInstances() {
  3. //GIVEN
  4. Scope scope = new ScopeImpl("");
  5. scope.installModules(new Module() {
  6. {
  7. bind(IFoo.class).toProvider(IFooWithBarProvider.class);
  8. }
  9. });
  10. //WHEN
  11. IFoo foo = scope.getInstance(IFoo.class);
  12. IFoo foo2 = scope.getInstance(IFoo.class);
  13. //THEN
  14. assertThat(foo, notNullValue());
  15. assertThat(foo2, not(sameInstance(foo)));
  16. assertThat(((Foo) foo).bar, notNullValue());
  17. assertThat(((Foo) foo2).bar, notNullValue());
  18. assertThat(((Foo) foo).bar, not(sameInstance(((Foo) foo2).bar)));
  19. }

代码示例来源:origin: stephanenicolas/toothpick

  1. @Test
  2. public void providerClassBinding_shouldCreateNonInjectedInstancesWithProviderSingleton_whenProviderClassIsAnnotatedSingleton() {
  3. //GIVEN
  4. Scope scope = new ScopeImpl("");
  5. scope.installModules(new Module() {
  6. {
  7. bind(IFoo.class).toProvider(FooProviderAnnotatedSingletonImpl.class);
  8. }
  9. });
  10. //WHEN
  11. IFoo foo = scope.getInstance(IFoo.class);
  12. IFoo foo2 = scope.getInstance(IFoo.class);
  13. //THEN
  14. assertThat(foo, notNullValue());
  15. assertThat(foo2, not(sameInstance(foo)));
  16. assertThat(((Foo) foo).bar, notNullValue());
  17. assertThat(((Foo) foo2).bar, notNullValue());
  18. assertThat(((Foo) foo).bar, sameInstance(((Foo) foo2).bar));
  19. }

代码示例来源:origin: stephanenicolas/toothpick

  1. @Test
  2. public void providerClassBinding_shouldCreateInstancesViaProviderInstances_whenInstancesInScopeViaCode() {
  3. //GIVEN
  4. Scope scope = new ScopeImpl("");
  5. scope.installModules(new Module() {
  6. {
  7. bind(Foo.class).toProvider(FooProviderReusingInstance.class).instancesInScope();
  8. }
  9. });
  10. //WHEN
  11. Foo foo = scope.getInstance(Foo.class);
  12. Foo foo2 = scope.getInstance(Foo.class);
  13. //THEN
  14. assertThat(foo, notNullValue());
  15. assertThat(foo2, not(sameInstance(foo)));
  16. }

代码示例来源:origin: stephanenicolas/toothpick

  1. @Test
  2. public void providerClassBinding_shouldCreateProviderSingleton_whenSingletonViaCode() {
  3. //GIVEN
  4. Scope scope = new ScopeImpl("");
  5. scope.installModules(new Module() {
  6. {
  7. bind(Foo.class).toProvider(FooProviderReusingInstance.class).singletonInScope();
  8. }
  9. });
  10. //WHEN
  11. Foo foo = scope.getInstance(Foo.class);
  12. Foo foo2 = scope.getInstance(Foo.class);
  13. //THEN
  14. assertThat(foo, notNullValue());
  15. assertThat(foo2, sameInstance(foo));
  16. }

代码示例来源:origin: stephanenicolas/toothpick

  1. @Test
  2. public void providerClassBinding_shouldProvideSingletons_whenProvidesSingletonViaCode() {
  3. //GIVEN
  4. Scope scope = new ScopeImpl("");
  5. scope.installModules(new Module() {
  6. {
  7. bind(Foo.class).toProvider(FooProvider.class).providesSingletonInScope();
  8. }
  9. });
  10. //WHEN
  11. Foo foo = scope.getInstance(Foo.class);
  12. Foo foo2 = scope.getInstance(Foo.class);
  13. //THEN
  14. assertThat(foo, notNullValue());
  15. assertThat(foo2, sameInstance(foo));
  16. }

代码示例来源:origin: stephanenicolas/toothpick

  1. @Test
  2. public void childInjector_shouldReturnInstancesInParentScopeUsingOnlyParentScope_whenProviderClassBindingIsCreatingInstancesInScope()
  3. throws Exception {
  4. //GIVEN
  5. Scope scopeParent = Toothpick.openScope("root");
  6. scopeParent.installModules(new Module() {
  7. {
  8. bind(IFoo.class).toProvider(IFooProvider.class).instancesInScope();
  9. }
  10. });
  11. Scope scope = Toothpick.openScopes("root", "child");
  12. scope.installModules(new Module() {
  13. {
  14. bind(Bar.class).to(BarChild.class);
  15. }
  16. });
  17. //WHEN
  18. IFoo instance = scope.getInstance(IFoo.class);
  19. //THEN
  20. assertThat(((Foo) instance).bar, instanceOf(Bar.class));
  21. assertThat(((Foo) instance).bar, not(instanceOf(BarChild.class)));
  22. }

代码示例来源:origin: stephanenicolas/toothpick

  1. @Test
  2. public void providerClassBinding_shouldCreateNonInjectedSingleton_whenProviderClassIsAnnotatedProvidesSingleton() {
  3. //GIVEN
  4. Scope scope = new ScopeImpl("");
  5. scope.bindScopeAnnotation(CustomScope.class);
  6. scope.installModules(new Module() {
  7. {
  8. bind(IFoo.class).toProvider(FooProviderAnnotatedProvidesSingleton.class);
  9. }
  10. });
  11. //WHEN
  12. IFoo foo = scope.getInstance(IFoo.class);
  13. IFoo foo2 = scope.getInstance(IFoo.class);
  14. //THEN
  15. assertThat(foo, notNullValue());
  16. assertThat(foo2, sameInstance(foo));
  17. }

代码示例来源:origin: stephanenicolas/toothpick

  1. @Test
  2. public void childInjector_shouldReturnInstancesInParentScopeUsingOnlyParentScope_whenProviderClassBindingIsCreatingSingletonInScope()
  3. throws Exception {
  4. //GIVEN
  5. Scope scopeParent = Toothpick.openScope("root");
  6. scopeParent.installModules(new Module() {
  7. {
  8. bind(IFoo.class).toProvider(IFooProvider.class).singletonInScope();
  9. }
  10. });
  11. Scope scope = Toothpick.openScopes("root", "child");
  12. scope.installModules(new Module() {
  13. {
  14. bind(Bar.class).to(BarChild.class);
  15. }
  16. });
  17. //WHEN
  18. IFoo instance = scope.getInstance(IFoo.class);
  19. //THEN
  20. assertThat(((Foo) instance).bar, instanceOf(Bar.class));
  21. assertThat(((Foo) instance).bar, not(instanceOf(BarChild.class)));
  22. }

代码示例来源:origin: stephanenicolas/toothpick

  1. @Test(expected = IllegalStateException.class)
  2. public void binding_shouldCrashForScopeAnnotatedClass_whenBindingToAProvider() throws Exception {
  3. //GIVEN
  4. Toothpick.setConfiguration(forDevelopment());
  5. Scope scope1 = Toothpick.openScopes("root", "scope1");
  6. //WHEN
  7. scope1.installModules(new Module() {
  8. {
  9. bind(IFoo.class).toProvider(FooProviderAnnotatedSingletonImpl.class);
  10. }
  11. });
  12. //THEN
  13. fail("This test should throw a IllegalBindingException.");
  14. }

代码示例来源:origin: com.github.stephanenicolas/toothpick-testing

  1. /**
  2. * Bind all {@code Mock} annotated field of a given test.
  3. *
  4. * @param test the test whose fields are going to be injected.
  5. */
  6. @SuppressWarnings("unchecked")
  7. public void bindAllMocks(Object test) {
  8. mockCount = 0;
  9. for (Field field : test.getClass().getDeclaredFields()) {
  10. Annotation mockAnnotation = findMockAnnotation(field);
  11. String injectionName = findInjectionName(field);
  12. if (mockAnnotation != null) {
  13. FieldValueProvider mockProvider = new FieldValueProvider(field, test);
  14. if (injectionName != null) {
  15. bind(field.getType()).withName(injectionName).toProvider(mockProvider);
  16. } else {
  17. bind(field.getType()).toProvider(mockProvider);
  18. }
  19. mockCount++;
  20. }
  21. }
  22. }

相关文章