1 16 17 package com.google.inject; 18 19 import junit.framework.TestCase; 20 21 24 public class ImplicitBindingTest extends TestCase { 25 26 public void testCircularDependency() throws CreationException { 27 Injector injector = Guice.createEmptyInjector(); 28 Foo foo = injector.getInstance(Foo.class); 29 assertSame(foo, foo.bar.foo); 30 } 31 32 static class Foo { 33 @Inject Bar bar; 34 } 35 36 static class Bar { 37 final Foo foo; 38 @Inject 39 public Bar(Foo foo) { 40 this.foo = foo; 41 } 42 } 43 44 public void testDefaultImplementation() { 45 Injector injector = Guice.createEmptyInjector(); 46 I i = injector.getInstance(I.class); 47 i.go(); 48 } 49 50 @ImplementedBy(IImpl.class) 51 interface I { 52 void go(); 53 } 54 55 static class IImpl implements I { 56 public void go() {} 57 } 58 59 public void testDefaultProvider() { 60 Injector injector = Guice.createEmptyInjector(); 61 Provided provided = injector.getInstance(Provided.class); 62 provided.go(); 63 } 64 65 @ProvidedBy(ProvidedProvider.class) 66 interface Provided { 67 void go(); 68 } 69 70 static class ProvidedProvider implements Provider<Provided> { 71 public Provided get() { 72 return new Provided() { 73 public void go() {} 74 }; 75 } 76 } 77 78 } 79 | Popular Tags |