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