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