Skip to content

Commit 69c9374

Browse files
committed
Added tests for model-view-controller pattern
1 parent a57a71b commit 69c9374

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed

model-view-controller/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@
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>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.iluwatar.model.view.controller;
2+
3+
import org.junit.Test;
4+
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.verifyNoMoreInteractions;
8+
import static org.mockito.Mockito.verifyZeroInteractions;
9+
10+
/**
11+
* Date: 12/20/15 - 2:19 PM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public class GiantControllerTest {
16+
17+
/**
18+
* Verify if the controller passes the health level through to the model and vice versa
19+
*/
20+
@Test
21+
public void testSetHealth() {
22+
final GiantModel model = mock(GiantModel.class);
23+
final GiantView view = mock(GiantView.class);
24+
final GiantController controller = new GiantController(model, view);
25+
26+
verifyZeroInteractions(model, view);
27+
28+
for (final Health health : Health.values()) {
29+
controller.setHealth(health);
30+
verify(model).setHealth(health);
31+
verifyZeroInteractions(view);
32+
}
33+
34+
controller.getHealth();
35+
verify(model).getHealth();
36+
37+
verifyNoMoreInteractions(model, view);
38+
}
39+
40+
/**
41+
* Verify if the controller passes the fatigue level through to the model and vice versa
42+
*/
43+
@Test
44+
public void testSetFatigue() {
45+
final GiantModel model = mock(GiantModel.class);
46+
final GiantView view = mock(GiantView.class);
47+
final GiantController controller = new GiantController(model, view);
48+
49+
verifyZeroInteractions(model, view);
50+
51+
for (final Fatigue fatigue : Fatigue.values()) {
52+
controller.setFatigue(fatigue);
53+
verify(model).setFatigue(fatigue);
54+
verifyZeroInteractions(view);
55+
}
56+
57+
controller.getFatigue();
58+
verify(model).getFatigue();
59+
60+
verifyNoMoreInteractions(model, view);
61+
}
62+
63+
/**
64+
* Verify if the controller passes the nourishment level through to the model and vice versa
65+
*/
66+
@Test
67+
public void testSetNourishment() {
68+
final GiantModel model = mock(GiantModel.class);
69+
final GiantView view = mock(GiantView.class);
70+
final GiantController controller = new GiantController(model, view);
71+
72+
verifyZeroInteractions(model, view);
73+
74+
for (final Nourishment nourishment : Nourishment.values()) {
75+
controller.setNourishment(nourishment);
76+
verify(model).setNourishment(nourishment);
77+
verifyZeroInteractions(view);
78+
}
79+
80+
controller.getNourishment();
81+
verify(model).getNourishment();
82+
83+
verifyNoMoreInteractions(model, view);
84+
}
85+
86+
@Test
87+
public void testUpdateView() {
88+
final GiantModel model = mock(GiantModel.class);
89+
final GiantView view = mock(GiantView.class);
90+
final GiantController controller = new GiantController(model, view);
91+
92+
verifyZeroInteractions(model, view);
93+
94+
controller.updateView();
95+
verify(view).displayGiant(model);
96+
97+
verifyNoMoreInteractions(model, view);
98+
}
99+
100+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.iluwatar.model.view.controller;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* Date: 12/20/15 - 2:10 PM
9+
*
10+
* @author Jeroen Meulemeester
11+
*/
12+
public class GiantModelTest {
13+
14+
/**
15+
* Verify if the health value is set properly though the constructor and setter
16+
*/
17+
@Test
18+
public void testSetHealth() {
19+
final GiantModel model = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED);
20+
assertEquals(Health.HEALTHY, model.getHealth());
21+
for (final Health health : Health.values()) {
22+
model.setHealth(health);
23+
assertEquals(health, model.getHealth());
24+
assertEquals("The giant looks " + health.toString() + ", alert and saturated.", model.toString());
25+
}
26+
}
27+
28+
/**
29+
* Verify if the fatigue level is set properly though the constructor and setter
30+
*/
31+
@Test
32+
public void testSetFatigue() {
33+
final GiantModel model = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED);
34+
assertEquals(Fatigue.ALERT, model.getFatigue());
35+
for (final Fatigue fatigue : Fatigue.values()) {
36+
model.setFatigue(fatigue);
37+
assertEquals(fatigue, model.getFatigue());
38+
assertEquals("The giant looks healthy, " + fatigue.toString() + " and saturated.", model.toString());
39+
}
40+
}
41+
42+
/**
43+
* Verify if the nourishment level is set properly though the constructor and setter
44+
*/
45+
@Test
46+
public void testSetNourishment() {
47+
final GiantModel model = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED);
48+
assertEquals(Nourishment.SATURATED, model.getNourishment());
49+
for (final Nourishment nourishment : Nourishment.values()) {
50+
model.setNourishment(nourishment);
51+
assertEquals(nourishment, model.getNourishment());
52+
assertEquals("The giant looks healthy, alert and " + nourishment.toString() + ".", model.toString());
53+
}
54+
}
55+
56+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.iluwatar.model.view.controller;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.PrintStream;
8+
9+
import static org.mockito.Mockito.mock;
10+
import static org.mockito.Mockito.verify;
11+
import static org.mockito.Mockito.verifyNoMoreInteractions;
12+
13+
/**
14+
* Date: 12/20/15 - 2:04 PM
15+
*
16+
* @author Jeroen Meulemeester
17+
*/
18+
public class GiantViewTest {
19+
20+
/**
21+
* The mocked standard out {@link PrintStream}, required since the actions of the views don't have
22+
* any influence on any other accessible objects, except for writing to std-out using {@link
23+
* System#out}
24+
*/
25+
private final PrintStream stdOutMock = mock(PrintStream.class);
26+
27+
/**
28+
* Keep the original std-out so it can be restored after the test
29+
*/
30+
private final PrintStream stdOutOrig = System.out;
31+
32+
/**
33+
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
34+
*/
35+
@Before
36+
public void setUp() {
37+
System.setOut(this.stdOutMock);
38+
}
39+
40+
/**
41+
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
42+
*/
43+
@After
44+
public void tearDown() {
45+
System.setOut(this.stdOutOrig);
46+
}
47+
48+
/**
49+
* Verify if the {@link GiantView} does what it has to do: Print the {@link GiantModel} to the
50+
* standard out stream, nothing more, nothing less.
51+
*/
52+
@Test
53+
public void testDisplayGiant() {
54+
final GiantView view = new GiantView();
55+
56+
final GiantModel model = mock(GiantModel.class);
57+
view.displayGiant(model);
58+
59+
verify(this.stdOutMock).println(model);
60+
verifyNoMoreInteractions(model, this.stdOutMock);
61+
62+
}
63+
64+
}

0 commit comments

Comments
 (0)