1+ package com .iluwatar .observer ;
2+
3+ import org .junit .Test ;
4+ import org .mockito .InOrder ;
5+
6+ import static org .mockito .Mockito .inOrder ;
7+ import static org .mockito .Mockito .mock ;
8+ import static org .mockito .Mockito .verify ;
9+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
10+ import static org .mockito .Mockito .verifyZeroInteractions ;
11+
12+ /**
13+ * Date: 12/27/15 - 11:08 AM
14+ *
15+ * @author Jeroen Meulemeester
16+ */
17+ public class WeatherTest extends StdOutTest {
18+
19+ /**
20+ * Add a {@link WeatherObserver}, verify if it gets notified of a weather change, remove the
21+ * observer again and verify that there are no more notifications.
22+ */
23+ @ Test
24+ public void testAddRemoveObserver () {
25+ final WeatherObserver observer = mock (WeatherObserver .class );
26+
27+ final Weather weather = new Weather ();
28+ weather .addObserver (observer );
29+ verifyZeroInteractions (observer );
30+
31+ weather .timePasses ();
32+ verify (getStdOutMock ()).println ("The weather changed to rainy." );
33+ verify (observer ).update (WeatherType .RAINY );
34+
35+ weather .removeObserver (observer );
36+ weather .timePasses ();
37+ verify (getStdOutMock ()).println ("The weather changed to windy." );
38+
39+ verifyNoMoreInteractions (observer , getStdOutMock ());
40+ }
41+
42+ /**
43+ * Verify if the weather passes in the order of the {@link WeatherType}s
44+ */
45+ @ Test
46+ public void testTimePasses () {
47+ final WeatherObserver observer = mock (WeatherObserver .class );
48+ final Weather weather = new Weather ();
49+ weather .addObserver (observer );
50+
51+ final InOrder inOrder = inOrder (observer , getStdOutMock ());
52+ final WeatherType [] weatherTypes = WeatherType .values ();
53+ for (int i = 1 ; i < 20 ; i ++) {
54+ weather .timePasses ();
55+ inOrder .verify (observer ).update (weatherTypes [i % weatherTypes .length ]);
56+ }
57+
58+ verifyNoMoreInteractions (observer );
59+ }
60+
61+ }
0 commit comments