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 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(
39 "com.google.inject.ConfigurationException")) {
40 System.out.println(e);
41 } else {
42 throw e;
43 }
44 }
45 }
46
47 @Test(expected = ConfigurationException.class)
48 public void getBinding1() {
49 Key<Foo> key = Key.get(Foo.class);
50 injector.getBinding(key);
51
52 }
53
54 @Test
55 public void getBinding2() {
56 Key<Bar> key = Key.get(Bar.class);
57 Binding<Bar> binding = injector.getBinding(key);
58 assertNotNull(binding);
59 }
60
61 @Test
62 public void getBinding3() {
63 Key<Baz> key = Key.get(Baz.class);
64 Binding<Baz> binding = injector.getBinding(key);
65
66 assertNotNull(binding);
67 }
68
69 private static class GuiceTestModule extends AbstractModule {
70
71 @Override
72 protected void configure() {
73 bind(Bar.class).to(BarImpl.class);
74 }
75
76 }
77
78 private static interface Foo {
79 }
80
81 private static interface Bar {
82 }
83
84 private static class BarImpl implements Bar {
85 }
86
87 private static class Baz {
88 }
89
90 }