1+ package com .iluwatar .dependency .injection ;
2+
3+ import com .google .inject .AbstractModule ;
4+ import com .google .inject .Guice ;
5+ import com .google .inject .Injector ;
6+
7+ import org .junit .Test ;
8+
9+ import static org .mockito .Mockito .times ;
10+ import static org .mockito .Mockito .verify ;
11+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
12+
13+ /**
14+ * Date: 12/10/15 - 8:57 PM
15+ *
16+ * @author Jeroen Meulemeester
17+ */
18+ public class GuiceWizardTest extends StdOutTest {
19+
20+ /**
21+ * Test if the {@link GuiceWizard} smokes whatever instance of {@link Tobacco} is passed to him
22+ * through the constructor parameter
23+ */
24+ @ Test
25+ public void testSmokeEveryThingThroughConstructor () throws Exception {
26+
27+ final Tobacco [] tobaccos = {
28+ new OldTobyTobacco (), new RivendellTobacco (), new SecondBreakfastTobacco ()
29+ };
30+
31+ for (final Tobacco tobacco : tobaccos ) {
32+ final GuiceWizard guiceWizard = new GuiceWizard (tobacco );
33+ guiceWizard .smoke ();
34+
35+ // Verify if the wizard is smoking the correct tobacco ...
36+ verify (getStdOutMock (), times (1 )).println ("GuiceWizard smoking " + tobacco .getClass ().getSimpleName ());
37+
38+ // ... and nothing else is happening.
39+ verifyNoMoreInteractions (getStdOutMock ());
40+ }
41+
42+ }
43+
44+ /**
45+ * Test if the {@link GuiceWizard} smokes whatever instance of {@link Tobacco} is passed to him
46+ * through the Guice google inject framework
47+ */
48+ @ Test
49+ public void testSmokeEveryThingThroughInjectionFramework () throws Exception {
50+
51+ @ SuppressWarnings ("unchecked" )
52+ final Class <? extends Tobacco >[] tobaccos = new Class []{
53+ OldTobyTobacco .class , RivendellTobacco .class , SecondBreakfastTobacco .class
54+ };
55+
56+ for (final Class <? extends Tobacco > tobaccoClass : tobaccos ) {
57+ // Configure the tobacco in the injection framework ...
58+ final Injector injector = Guice .createInjector (new AbstractModule () {
59+ @ Override
60+ protected void configure () {
61+ bind (Tobacco .class ).to (tobaccoClass );
62+ }
63+ });
64+
65+ // ... and create a new wizard with it
66+ final GuiceWizard guiceWizard = injector .getInstance (GuiceWizard .class );
67+ guiceWizard .smoke ();
68+
69+ // Verify if the wizard is smoking the correct tobacco ...
70+ verify (getStdOutMock (), times (1 )).println ("GuiceWizard smoking " + tobaccoClass .getSimpleName ());
71+
72+ // ... and nothing else is happening.
73+ verifyNoMoreInteractions (getStdOutMock ());
74+ }
75+
76+ }
77+
78+ }
0 commit comments