1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.plugins.guice.spyke;
17
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.assertNull;
20
21 import org.junit.Test;
22
23 import com.google.inject.AbstractModule;
24 import com.google.inject.Binding;
25 import com.google.inject.Guice;
26 import com.google.inject.Injector;
27 import com.google.inject.Key;
28
29 public class GuiceTest {
30
31 private Injector injector = Guice.createInjector(new GuiceTestModule());
32
33 @Test
34 public void getInstance() {
35 try {
36 injector.getInstance(Foo.class);
37 } catch (RuntimeException e) {
38 if (e.getClass().getName().equals("com.google.inject.ConfigurationException")) {
39 System.out.println(e);
40 } else {
41 throw e;
42 }
43 }
44 }
45
46 @Test
47 public void getBinding1() {
48 Key<Foo> key = Key.get(Foo.class);
49 Binding<Foo> binding = injector.getBinding(key);
50 assertNull(binding);
51 }
52
53 @Test
54 public void getBinding2() {
55 Key<Bar> key = Key.get(Bar.class);
56 Binding<Bar> binding = injector.getBinding(key);
57 assertNotNull(binding);
58 }
59
60 @Test
61 public void getBinding3() {
62 Key<Baz> key = Key.get(Baz.class);
63 Binding<Baz> binding = injector.getBinding(key);
64 assertNull(binding);
65 }
66
67 private static class GuiceTestModule extends AbstractModule {
68
69 @Override
70 protected void configure() {
71 bind(Bar.class).to(BarImpl.class);
72 }
73
74 }
75
76 private static interface Foo {
77 }
78
79 private static interface Bar {
80 }
81
82 private static class BarImpl implements Bar {
83 }
84
85 private static class Baz {
86 }
87
88 }