1+ package com .iluwatar .state ;
2+
3+ import org .junit .After ;
4+ import org .junit .Before ;
5+ import org .junit .Test ;
6+ import org .mockito .InOrder ;
7+ import org .mockito .Mockito ;
8+
9+ import java .io .PrintStream ;
10+
11+ import static org .junit .Assert .assertEquals ;
12+ import static org .junit .Assert .assertNotNull ;
13+ import static org .mockito .Mockito .mock ;
14+
15+ /**
16+ * Date: 12/29/15 - 8:27 PM
17+ *
18+ * @author Jeroen Meulemeester
19+ */
20+ public class MammothTest {
21+
22+ /**
23+ * The mocked standard out {@link PrintStream}, required since some actions don't have any
24+ * influence on accessible objects, except for writing to std-out using {@link System#out}
25+ */
26+ private final PrintStream stdOutMock = mock (PrintStream .class );
27+
28+ /**
29+ * Keep the original std-out so it can be restored after the test
30+ */
31+ private final PrintStream stdOutOrig = System .out ;
32+
33+ /**
34+ * Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
35+ */
36+ @ Before
37+ public void setUp () {
38+ System .setOut (this .stdOutMock );
39+ }
40+
41+ /**
42+ * Removed the mocked std-out {@link PrintStream} again from the {@link System} class
43+ */
44+ @ After
45+ public void tearDown () {
46+ System .setOut (this .stdOutOrig );
47+ }
48+
49+ /**
50+ * Switch to a complete mammoth 'mood'-cycle and verify if the observed mood matches the expected
51+ * value.
52+ */
53+ @ Test
54+ public void testTimePasses () {
55+ final InOrder inOrder = Mockito .inOrder (this .stdOutMock );
56+ final Mammoth mammoth = new Mammoth ();
57+
58+ mammoth .observe ();
59+ inOrder .verify (this .stdOutMock ).println ("The mammoth is calm and peaceful." );
60+ inOrder .verifyNoMoreInteractions ();
61+
62+ mammoth .timePasses ();
63+ inOrder .verify (this .stdOutMock ).println ("The mammoth gets angry!" );
64+ inOrder .verifyNoMoreInteractions ();
65+
66+ mammoth .observe ();
67+ inOrder .verify (this .stdOutMock ).println ("The mammoth is furious!" );
68+ inOrder .verifyNoMoreInteractions ();
69+
70+ mammoth .timePasses ();
71+ inOrder .verify (this .stdOutMock ).println ("The mammoth calms down." );
72+ inOrder .verifyNoMoreInteractions ();
73+
74+ mammoth .observe ();
75+ inOrder .verify (this .stdOutMock ).println ("The mammoth is calm and peaceful." );
76+ inOrder .verifyNoMoreInteractions ();
77+
78+ }
79+
80+ /**
81+ * Verify if {@link Mammoth#toString()} gives the expected value
82+ */
83+ @ Test
84+ public void testToString () {
85+ final String toString = new Mammoth ().toString ();
86+ assertNotNull (toString );
87+ assertEquals ("The mammoth" , toString );
88+ }
89+
90+ }
0 commit comments