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
20 import org.junit.Test;
21
22 import com.google.inject.AbstractModule;
23 import com.google.inject.Binding;
24 import com.google.inject.ConfigurationException;
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 final Injector injector = Guice
32 .createInjector(new GuiceTestModule());
33
34 @Test
35 public void getInstance() {
36 try {
37 injector.getInstance(Foo.class);
38 } catch (final RuntimeException e) {
39 if (e.getClass().getName().equals(
40 "com.google.inject.ConfigurationException")) {
41 System.out.println(e);
42 } else {
43 throw e;
44 }
45 }
46 }
47
48 @Test(expected = ConfigurationException.class)
49 public void getBinding1() {
50 final Key<Foo> key = Key.get(Foo.class);
51 injector.getBinding(key);
52
53 }
54
55 @Test
56 public void getBinding2() {
57 final Key<Bar> key = Key.get(Bar.class);
58 final Binding<Bar> binding = injector.getBinding(key);
59 assertNotNull(binding);
60 }
61
62 @Test
63 public void getBinding3() {
64 final Key<Baz> key = Key.get(Baz.class);
65 final Binding<Baz> binding = injector.getBinding(key);
66
67 assertNotNull(binding);
68 }
69
70 private static class GuiceTestModule extends AbstractModule {
71
72 @Override
73 protected void configure() {
74 bind(Bar.class).to(BarImpl.class);
75 }
76
77 }
78
79 private static interface Foo {
80 }
81
82 private static interface Bar {
83 }
84
85 private static class BarImpl implements Bar {
86 }
87
88 private static class Baz {
89 }
90
91 }