Skip to content

Commit 29fc560

Browse files
committed
Add proper unit tests for dependency-injection pattern
1 parent 0643289 commit 29fc560

File tree

5 files changed

+209
-0
lines changed

5 files changed

+209
-0
lines changed

dependency-injection/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
<groupId>junit</groupId>
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-core</artifactId>
20+
<scope>test</scope>
1621
</dependency>
1722
<dependency>
1823
<groupId>com.google.inject</groupId>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.iluwatar.dependency.injection;
2+
3+
import org.junit.Test;
4+
5+
import static org.mockito.Mockito.times;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.verifyNoMoreInteractions;
8+
9+
/**
10+
* Date: 12/10/15 - 8:40 PM
11+
*
12+
* @author Jeroen Meulemeester
13+
*/
14+
public class AdvancedWizardTest extends StdOutTest {
15+
16+
/**
17+
* Test if the {@link AdvancedWizard} smokes whatever instance of {@link Tobacco} is passed to him
18+
* through the constructor parameter
19+
*/
20+
@Test
21+
public void testSmokeEveryThing() throws Exception {
22+
23+
final Tobacco[] tobaccos = {
24+
new OldTobyTobacco(), new RivendellTobacco(), new SecondBreakfastTobacco()
25+
};
26+
27+
for (final Tobacco tobacco : tobaccos) {
28+
final AdvancedWizard advancedWizard = new AdvancedWizard(tobacco);
29+
advancedWizard.smoke();
30+
31+
// Verify if the wizard is smoking the correct tobacco ...
32+
verify(getStdOutMock(), times(1)).println("AdvancedWizard smoking " + tobacco.getClass().getSimpleName());
33+
34+
// ... and nothing else is happening.
35+
verifyNoMoreInteractions(getStdOutMock());
36+
}
37+
38+
}
39+
40+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.iluwatar.dependency.injection;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.PrintStream;
8+
9+
import static org.mockito.Mockito.mock;
10+
import static org.mockito.Mockito.times;
11+
import static org.mockito.Mockito.verify;
12+
import static org.mockito.Mockito.verifyNoMoreInteractions;
13+
14+
/**
15+
* Date: 12/10/15 - 8:26 PM
16+
*
17+
* @author Jeroen Meulemeester
18+
*/
19+
public class SimpleWizardTest extends StdOutTest {
20+
21+
/**
22+
* Test if the {@link SimpleWizard} does the only thing it can do: Smoke it's {@link
23+
* OldTobyTobacco}
24+
*/
25+
@Test
26+
public void testSmoke() {
27+
final SimpleWizard simpleWizard = new SimpleWizard();
28+
simpleWizard.smoke();
29+
verify(getStdOutMock(), times(1)).println("SimpleWizard smoking OldTobyTobacco");
30+
verifyNoMoreInteractions(getStdOutMock());
31+
}
32+
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.iluwatar.dependency.injection;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
6+
import java.io.PrintStream;
7+
8+
import static org.mockito.Mockito.mock;
9+
10+
/**
11+
* Date: 12/10/15 - 8:37 PM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public abstract class StdOutTest {
16+
17+
/**
18+
* The mocked standard out {@link PrintStream}, required since the actions of the wizard don't
19+
* have any influence on any other accessible objects, except for writing to std-out using {@link
20+
* System#out}
21+
*/
22+
private final PrintStream stdOutMock = mock(PrintStream.class);
23+
24+
/**
25+
* Keep the original std-out so it can be restored after the test
26+
*/
27+
private final PrintStream stdOutOrig = System.out;
28+
29+
/**
30+
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
31+
*/
32+
@Before
33+
public void setUp() {
34+
System.setOut(this.stdOutMock);
35+
}
36+
37+
/**
38+
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
39+
*/
40+
@After
41+
public void tearDown() {
42+
System.setOut(this.stdOutOrig);
43+
}
44+
45+
/**
46+
* Get the mocked stdOut {@link PrintStream}
47+
*
48+
* @return The stdOut print stream mock, renewed before each test
49+
*/
50+
final PrintStream getStdOutMock() {
51+
return this.stdOutMock;
52+
}
53+
}

0 commit comments

Comments
 (0)