File tree Expand file tree Collapse file tree 3 files changed +85
-0
lines changed
resource-acquisition-is-initialization
src/test/java/com/iluwatar/resource/acquisition/is/initialization Expand file tree Collapse file tree 3 files changed +85
-0
lines changed Original file line number Diff line number Diff line change 1414 <artifactId >junit</artifactId >
1515 <scope >test</scope >
1616 </dependency >
17+ <dependency >
18+ <groupId >org.mockito</groupId >
19+ <artifactId >mockito-core</artifactId >
20+ <scope >test</scope >
21+ </dependency >
1722 </dependencies >
1823</project >
Original file line number Diff line number Diff line change 1+ package com .iluwatar .resource .acquisition .is .initialization ;
2+
3+ import org .junit .Test ;
4+ import org .mockito .InOrder ;
5+
6+ import static org .mockito .Mockito .inOrder ;
7+
8+ /**
9+ * Date: 12/28/15 - 9:31 PM
10+ *
11+ * @author Jeroen Meulemeester
12+ */
13+ public class ClosableTest extends StdOutTest {
14+
15+ @ Test
16+ public void testOpenClose () throws Exception {
17+ final InOrder inOrder = inOrder (getStdOutMock ());
18+ try (final SlidingDoor door = new SlidingDoor (); final TreasureChest chest = new TreasureChest ()) {
19+ inOrder .verify (getStdOutMock ()).println ("Sliding door opens." );
20+ inOrder .verify (getStdOutMock ()).println ("Treasure chest opens." );
21+ }
22+ inOrder .verify (getStdOutMock ()).println ("Treasure chest closes." );
23+ inOrder .verify (getStdOutMock ()).println ("Sliding door closes." );
24+ inOrder .verifyNoMoreInteractions ();
25+ }
26+
27+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar .resource .acquisition .is .initialization ;
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 some actions don't have any
19+ * influence on accessible objects, except for writing to std-out using {@link System#out}
20+ */
21+ private final PrintStream stdOutMock = mock (PrintStream .class );
22+
23+ /**
24+ * Keep the original std-out so it can be restored after the test
25+ */
26+ private final PrintStream stdOutOrig = System .out ;
27+
28+ /**
29+ * Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
30+ */
31+ @ Before
32+ public void setUp () {
33+ System .setOut (this .stdOutMock );
34+ }
35+
36+ /**
37+ * Removed the mocked std-out {@link PrintStream} again from the {@link System} class
38+ */
39+ @ After
40+ public void tearDown () {
41+ System .setOut (this .stdOutOrig );
42+ }
43+
44+ /**
45+ * Get the mocked stdOut {@link PrintStream}
46+ *
47+ * @return The stdOut print stream mock, renewed before each test
48+ */
49+ final PrintStream getStdOutMock () {
50+ return this .stdOutMock ;
51+ }
52+
53+ }
You can’t perform that action at this time.
0 commit comments