1 16 17 package com.google.inject; 18 19 import junit.framework.TestCase; 20 21 24 public class CircularDependencyTest extends TestCase { 25 26 public void testCircularlyDependentConstructors() 27 throws CreationException { 28 BinderImpl builder = new BinderImpl(); 29 builder.bind(A.class).to(AImpl.class); 30 builder.bind(B.class).to(BImpl.class); 31 32 Injector injector = builder.createInjector(); 33 A a = injector.getInstance(A.class); 34 assertNotNull(a.getB().getA()); 35 } 36 37 interface A { 38 B getB(); 39 } 40 41 @Singleton 42 static class AImpl implements A { 43 final B b; 44 @Inject public AImpl(B b) { 45 this.b = b; 46 } 47 public B getB() { 48 return b; 49 } 50 } 51 52 interface B { 53 A getA(); 54 } 55 56 static class BImpl implements B { 57 final A a; 58 @Inject public BImpl(A a) { 59 this.a = a; 60 } 61 public A getA() { 62 return a; 63 } 64 } 65 66 } 67 | Popular Tags |