1 16 17 package com.google.inject; 18 19 import junit.framework.TestCase; 20 21 24 public class NotRequiredTest extends TestCase { 25 26 public void testProvided() throws CreationException { 27 BinderImpl builder = new BinderImpl(); 28 builder.bind(Bar.class).to(BarImpl.class); 29 Injector c = builder.createInjector(); 30 Foo foo = c.getInstance(Foo.class); 31 assertNotNull(foo.bar); 32 assertNotNull(foo.fromMethod); 33 } 34 35 public void testNotProvided() throws CreationException { 36 Injector c = Guice.createInjector(new AbstractModule() { 37 protected void configure() { 38 bind(Foo.class); 39 } 40 }); 41 Foo foo = c.getInstance(Foo.class); 42 assertNull(foo.bar); 43 assertNull(foo.fromMethod); 44 } 45 46 static class Foo { 47 @Inject(optional=true) Bar bar; 48 49 Bar fromMethod; 50 51 @Inject(optional=true) void setBar(Bar bar) { 52 fromMethod = bar; 53 } 54 } 55 56 interface Bar {} 57 58 static class BarImpl implements Bar {} 59 } 60 | Popular Tags |