Skip to content

Commit 10a94cc

Browse files
committed
Add proper unit tests for decorator pattern
1 parent 8367c83 commit 10a94cc

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

decorator/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-core</artifactId>
20+
</dependency>
1721
</dependencies>
1822
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.iluwatar.decorator;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
import static org.mockito.Mockito.spy;
7+
import static org.mockito.Mockito.verify;
8+
import static org.mockito.Mockito.verifyNoMoreInteractions;
9+
import static org.mockito.internal.verification.VerificationModeFactory.times;
10+
11+
/**
12+
* Date: 12/7/15 - 7:47 PM
13+
*
14+
* @author Jeroen Meulemeester
15+
*/
16+
public class SmartTrollTest {
17+
18+
@Test
19+
public void testSmartTroll() throws Exception {
20+
// Create a normal troll first, but make sure we can spy on it later on.
21+
final Hostile simpleTroll = spy(new Troll());
22+
23+
// Now we want to decorate the troll to make it smarter ...
24+
final Hostile smartTroll = new SmartTroll(simpleTroll);
25+
assertEquals(30, smartTroll.getAttackPower());
26+
verify(simpleTroll, times(1)).getAttackPower();
27+
28+
// Check if the smart troll actions are delegated to the decorated troll
29+
smartTroll.attack();
30+
verify(simpleTroll, times(1)).attack();
31+
32+
smartTroll.fleeBattle();
33+
verify(simpleTroll, times(1)).fleeBattle();
34+
verifyNoMoreInteractions(simpleTroll);
35+
36+
}
37+
38+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.iluwatar.decorator;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.FileDescriptor;
8+
import java.io.FileOutputStream;
9+
import java.io.PrintStream;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.mockito.Matchers.eq;
13+
import static org.mockito.Mockito.mock;
14+
import static org.mockito.Mockito.verify;
15+
import static org.mockito.Mockito.verifyNoMoreInteractions;
16+
import static org.mockito.internal.verification.VerificationModeFactory.times;
17+
18+
/**
19+
* Date: 12/7/15 - 7:26 PM
20+
*
21+
* @author Jeroen Meulemeester
22+
*/
23+
public class TrollTest {
24+
25+
/**
26+
* The mocked standard out stream, required since the actions don't have any influence on other
27+
* objects, except for writing to the std-out using {@link System#out}
28+
*/
29+
private final PrintStream stdOutMock = mock(PrintStream.class);
30+
31+
/**
32+
* Keep the original std-out so it can be restored after the test
33+
*/
34+
private final PrintStream stdOutOrig = System.out;
35+
36+
/**
37+
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
38+
*/
39+
@Before
40+
public void setUp() {
41+
System.setOut(this.stdOutMock);
42+
}
43+
44+
/**
45+
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
46+
*/
47+
@After
48+
public void tearDown() {
49+
System.setOut(this.stdOutOrig);
50+
}
51+
52+
@Test
53+
public void testTrollActions() throws Exception {
54+
final Troll troll = new Troll();
55+
assertEquals(10, troll.getAttackPower());
56+
57+
troll.attack();
58+
verify(this.stdOutMock, times(1)).println(eq("The troll swings at you with a club!"));
59+
60+
troll.fleeBattle();
61+
verify(this.stdOutMock, times(1)).println(eq("The troll shrieks in horror and runs away!"));
62+
63+
verifyNoMoreInteractions(this.stdOutMock);
64+
}
65+
66+
}

0 commit comments

Comments
 (0)