Hi all,
It is strange when I'm writing this unit test to validate my comprehension
of BindingScopingVisitor I don't get my expecting result...
Is somebody can help me please?
Thanks in advance,
The code:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import javax.inject.Singleton;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import com.google.common.base.Supplier;
import com.google.inject.*;
import com.google.inject.name.Names;
import com.google.inject.spi.DefaultBindingScopingVisitor;
/**
* @author Romain Gilles Date: 4/17/13 Time: 10:40 PM
*/
@RunWith(MockitoJUnitRunner.class)
public class GuiceBindingScopingVisitorTest {
@Mock
Supplier<Object> supplier;
@Test
public void testScopingVisitor() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(MyInterface.class).to(MyClass.class);
bind(Key.get(MyInterface.class,
Names.named("toto"))).to(MyClass.class).in(com.google.inject.Singleton.class);
bind(Key.get(MyInterface.class,
Names.named("toto1"))).to(MyClass.class).in(Singleton.class);
bind(Key.get(MyInterface.class,
Names.named("toto2"))).to(MyClass.class).in(Scopes.SINGLETON);
}
});
assertThat(injector.getScopeBindings(), is(not(nullValue())));
for (Binding<?> binding : injector.getBindings().values()) {
binding.acceptScopingVisitor(new
DefaultBindingScopingVisitor<Object>() {
@Override
public Object visitScope(Scope scope) {
if (scope == Scopes.SINGLETON) {
supplier.get();
}
return scope;
}
});
}
verify(supplier, times(4)).get();
}
public static interface MyInterface {
}
@Singleton
public static class MyClass implements MyInterface {
}
}
The result:
org.mockito.exceptions.verification.TooLittleActualInvocations:
supplier.get();
Wanted 4 times:
-> at
test.GuiceBindingScopingVisitorTest.testScopingVisitor(GuiceBindingScopingVisitorTest.java:54)
But was 3 times:
-> at
test.GuiceBindingScopingVisitorTest$2.visitScope(GuiceBindingScopingVisitorTest.java:48)
My conclusion:
It seems that @Singleton annotation is not handle as an Scope but I think
it is a bug not?
Regards,
Romain
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.