本文整理了Java中toothpick.config.Binding.toProvider()
方法的一些代码示例,展示了Binding.toProvider()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Binding.toProvider()
方法的具体详情如下:
包路径:toothpick.config.Binding
类名称:Binding
方法名:toProvider
暂无
代码示例来源:origin: stephanenicolas/toothpick
@Test
public void providerClassBinding_shouldCreateInstancesViaProviderInstances_whenProviderClassIsNotAnnotated() {
//GIVEN
Scope scope = new ScopeImpl("");
scope.installModules(new Module() {
{
bind(IFoo.class).toProvider(IFooProvider.class);
}
});
//WHEN
IFoo foo = scope.getInstance(IFoo.class);
IFoo foo2 = scope.getInstance(IFoo.class);
//THEN
assertThat(foo, notNullValue());
assertThat(foo2, not(sameInstance(foo)));
assertThat(((Foo) foo).bar, notNullValue());
assertThat(((Foo) foo2).bar, notNullValue());
assertThat(((Foo) foo).bar, not(sameInstance(((Foo) foo2).bar)));
}
代码示例来源:origin: stephanenicolas/toothpick
@Test
public void providerClassBinding_shouldCreateInjectedProviderInstances() {
//GIVEN
Scope scope = new ScopeImpl("");
scope.installModules(new Module() {
{
bind(IFoo.class).toProvider(IFooWithBarProvider.class);
}
});
//WHEN
IFoo foo = scope.getInstance(IFoo.class);
IFoo foo2 = scope.getInstance(IFoo.class);
//THEN
assertThat(foo, notNullValue());
assertThat(foo2, not(sameInstance(foo)));
assertThat(((Foo) foo).bar, notNullValue());
assertThat(((Foo) foo2).bar, notNullValue());
assertThat(((Foo) foo).bar, not(sameInstance(((Foo) foo2).bar)));
}
代码示例来源:origin: stephanenicolas/toothpick
@Test
public void providerClassBinding_shouldCreateNonInjectedInstancesWithProviderSingleton_whenProviderClassIsAnnotatedSingleton() {
//GIVEN
Scope scope = new ScopeImpl("");
scope.installModules(new Module() {
{
bind(IFoo.class).toProvider(FooProviderAnnotatedSingletonImpl.class);
}
});
//WHEN
IFoo foo = scope.getInstance(IFoo.class);
IFoo foo2 = scope.getInstance(IFoo.class);
//THEN
assertThat(foo, notNullValue());
assertThat(foo2, not(sameInstance(foo)));
assertThat(((Foo) foo).bar, notNullValue());
assertThat(((Foo) foo2).bar, notNullValue());
assertThat(((Foo) foo).bar, sameInstance(((Foo) foo2).bar));
}
代码示例来源:origin: stephanenicolas/toothpick
@Test
public void providerClassBinding_shouldCreateInstancesViaProviderInstances_whenInstancesInScopeViaCode() {
//GIVEN
Scope scope = new ScopeImpl("");
scope.installModules(new Module() {
{
bind(Foo.class).toProvider(FooProviderReusingInstance.class).instancesInScope();
}
});
//WHEN
Foo foo = scope.getInstance(Foo.class);
Foo foo2 = scope.getInstance(Foo.class);
//THEN
assertThat(foo, notNullValue());
assertThat(foo2, not(sameInstance(foo)));
}
代码示例来源:origin: stephanenicolas/toothpick
@Test
public void providerClassBinding_shouldCreateProviderSingleton_whenSingletonViaCode() {
//GIVEN
Scope scope = new ScopeImpl("");
scope.installModules(new Module() {
{
bind(Foo.class).toProvider(FooProviderReusingInstance.class).singletonInScope();
}
});
//WHEN
Foo foo = scope.getInstance(Foo.class);
Foo foo2 = scope.getInstance(Foo.class);
//THEN
assertThat(foo, notNullValue());
assertThat(foo2, sameInstance(foo));
}
代码示例来源:origin: stephanenicolas/toothpick
@Test
public void providerClassBinding_shouldProvideSingletons_whenProvidesSingletonViaCode() {
//GIVEN
Scope scope = new ScopeImpl("");
scope.installModules(new Module() {
{
bind(Foo.class).toProvider(FooProvider.class).providesSingletonInScope();
}
});
//WHEN
Foo foo = scope.getInstance(Foo.class);
Foo foo2 = scope.getInstance(Foo.class);
//THEN
assertThat(foo, notNullValue());
assertThat(foo2, sameInstance(foo));
}
代码示例来源:origin: stephanenicolas/toothpick
@Test
public void childInjector_shouldReturnInstancesInParentScopeUsingOnlyParentScope_whenProviderClassBindingIsCreatingInstancesInScope()
throws Exception {
//GIVEN
Scope scopeParent = Toothpick.openScope("root");
scopeParent.installModules(new Module() {
{
bind(IFoo.class).toProvider(IFooProvider.class).instancesInScope();
}
});
Scope scope = Toothpick.openScopes("root", "child");
scope.installModules(new Module() {
{
bind(Bar.class).to(BarChild.class);
}
});
//WHEN
IFoo instance = scope.getInstance(IFoo.class);
//THEN
assertThat(((Foo) instance).bar, instanceOf(Bar.class));
assertThat(((Foo) instance).bar, not(instanceOf(BarChild.class)));
}
代码示例来源:origin: stephanenicolas/toothpick
@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));
}
代码示例来源:origin: stephanenicolas/toothpick
@Test
public void childInjector_shouldReturnInstancesInParentScopeUsingOnlyParentScope_whenProviderClassBindingIsCreatingSingletonInScope()
throws Exception {
//GIVEN
Scope scopeParent = Toothpick.openScope("root");
scopeParent.installModules(new Module() {
{
bind(IFoo.class).toProvider(IFooProvider.class).singletonInScope();
}
});
Scope scope = Toothpick.openScopes("root", "child");
scope.installModules(new Module() {
{
bind(Bar.class).to(BarChild.class);
}
});
//WHEN
IFoo instance = scope.getInstance(IFoo.class);
//THEN
assertThat(((Foo) instance).bar, instanceOf(Bar.class));
assertThat(((Foo) instance).bar, not(instanceOf(BarChild.class)));
}
代码示例来源:origin: stephanenicolas/toothpick
@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.");
}
代码示例来源:origin: com.github.stephanenicolas/toothpick-testing
/**
* Bind all {@code Mock} annotated field of a given test.
*
* @param test the test whose fields are going to be injected.
*/
@SuppressWarnings("unchecked")
public void bindAllMocks(Object test) {
mockCount = 0;
for (Field field : test.getClass().getDeclaredFields()) {
Annotation mockAnnotation = findMockAnnotation(field);
String injectionName = findInjectionName(field);
if (mockAnnotation != null) {
FieldValueProvider mockProvider = new FieldValueProvider(field, test);
if (injectionName != null) {
bind(field.getType()).withName(injectionName).toProvider(mockProvider);
} else {
bind(field.getType()).toProvider(mockProvider);
}
mockCount++;
}
}
}
内容来源于网络,如有侵权,请联系作者删除!