forked from iluwatar/java-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for model-view-controller pattern
- Loading branch information
Showing
4 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...view-controller/src/test/java/com/iluwatar/model/view/controller/GiantControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.iluwatar.model.view.controller; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.verifyZeroInteractions; | ||
|
||
/** | ||
* Date: 12/20/15 - 2:19 PM | ||
* | ||
* @author Jeroen Meulemeester | ||
*/ | ||
public class GiantControllerTest { | ||
|
||
/** | ||
* Verify if the controller passes the health level through to the model and vice versa | ||
*/ | ||
@Test | ||
public void testSetHealth() { | ||
final GiantModel model = mock(GiantModel.class); | ||
final GiantView view = mock(GiantView.class); | ||
final GiantController controller = new GiantController(model, view); | ||
|
||
verifyZeroInteractions(model, view); | ||
|
||
for (final Health health : Health.values()) { | ||
controller.setHealth(health); | ||
verify(model).setHealth(health); | ||
verifyZeroInteractions(view); | ||
} | ||
|
||
controller.getHealth(); | ||
verify(model).getHealth(); | ||
|
||
verifyNoMoreInteractions(model, view); | ||
} | ||
|
||
/** | ||
* Verify if the controller passes the fatigue level through to the model and vice versa | ||
*/ | ||
@Test | ||
public void testSetFatigue() { | ||
final GiantModel model = mock(GiantModel.class); | ||
final GiantView view = mock(GiantView.class); | ||
final GiantController controller = new GiantController(model, view); | ||
|
||
verifyZeroInteractions(model, view); | ||
|
||
for (final Fatigue fatigue : Fatigue.values()) { | ||
controller.setFatigue(fatigue); | ||
verify(model).setFatigue(fatigue); | ||
verifyZeroInteractions(view); | ||
} | ||
|
||
controller.getFatigue(); | ||
verify(model).getFatigue(); | ||
|
||
verifyNoMoreInteractions(model, view); | ||
} | ||
|
||
/** | ||
* Verify if the controller passes the nourishment level through to the model and vice versa | ||
*/ | ||
@Test | ||
public void testSetNourishment() { | ||
final GiantModel model = mock(GiantModel.class); | ||
final GiantView view = mock(GiantView.class); | ||
final GiantController controller = new GiantController(model, view); | ||
|
||
verifyZeroInteractions(model, view); | ||
|
||
for (final Nourishment nourishment : Nourishment.values()) { | ||
controller.setNourishment(nourishment); | ||
verify(model).setNourishment(nourishment); | ||
verifyZeroInteractions(view); | ||
} | ||
|
||
controller.getNourishment(); | ||
verify(model).getNourishment(); | ||
|
||
verifyNoMoreInteractions(model, view); | ||
} | ||
|
||
@Test | ||
public void testUpdateView() { | ||
final GiantModel model = mock(GiantModel.class); | ||
final GiantView view = mock(GiantView.class); | ||
final GiantController controller = new GiantController(model, view); | ||
|
||
verifyZeroInteractions(model, view); | ||
|
||
controller.updateView(); | ||
verify(view).displayGiant(model); | ||
|
||
verifyNoMoreInteractions(model, view); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
model-view-controller/src/test/java/com/iluwatar/model/view/controller/GiantModelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.iluwatar.model.view.controller; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Date: 12/20/15 - 2:10 PM | ||
* | ||
* @author Jeroen Meulemeester | ||
*/ | ||
public class GiantModelTest { | ||
|
||
/** | ||
* Verify if the health value is set properly though the constructor and setter | ||
*/ | ||
@Test | ||
public void testSetHealth() { | ||
final GiantModel model = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED); | ||
assertEquals(Health.HEALTHY, model.getHealth()); | ||
for (final Health health : Health.values()) { | ||
model.setHealth(health); | ||
assertEquals(health, model.getHealth()); | ||
assertEquals("The giant looks " + health.toString() + ", alert and saturated.", model.toString()); | ||
} | ||
} | ||
|
||
/** | ||
* Verify if the fatigue level is set properly though the constructor and setter | ||
*/ | ||
@Test | ||
public void testSetFatigue() { | ||
final GiantModel model = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED); | ||
assertEquals(Fatigue.ALERT, model.getFatigue()); | ||
for (final Fatigue fatigue : Fatigue.values()) { | ||
model.setFatigue(fatigue); | ||
assertEquals(fatigue, model.getFatigue()); | ||
assertEquals("The giant looks healthy, " + fatigue.toString() + " and saturated.", model.toString()); | ||
} | ||
} | ||
|
||
/** | ||
* Verify if the nourishment level is set properly though the constructor and setter | ||
*/ | ||
@Test | ||
public void testSetNourishment() { | ||
final GiantModel model = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED); | ||
assertEquals(Nourishment.SATURATED, model.getNourishment()); | ||
for (final Nourishment nourishment : Nourishment.values()) { | ||
model.setNourishment(nourishment); | ||
assertEquals(nourishment, model.getNourishment()); | ||
assertEquals("The giant looks healthy, alert and " + nourishment.toString() + ".", model.toString()); | ||
} | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
model-view-controller/src/test/java/com/iluwatar/model/view/controller/GiantViewTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.iluwatar.model.view.controller; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.PrintStream; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
|
||
/** | ||
* Date: 12/20/15 - 2:04 PM | ||
* | ||
* @author Jeroen Meulemeester | ||
*/ | ||
public class GiantViewTest { | ||
|
||
/** | ||
* The mocked standard out {@link PrintStream}, required since the actions of the views don't have | ||
* any influence on any other accessible objects, except for writing to std-out using {@link | ||
* System#out} | ||
*/ | ||
private final PrintStream stdOutMock = mock(PrintStream.class); | ||
|
||
/** | ||
* Keep the original std-out so it can be restored after the test | ||
*/ | ||
private final PrintStream stdOutOrig = System.out; | ||
|
||
/** | ||
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test | ||
*/ | ||
@Before | ||
public void setUp() { | ||
System.setOut(this.stdOutMock); | ||
} | ||
|
||
/** | ||
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class | ||
*/ | ||
@After | ||
public void tearDown() { | ||
System.setOut(this.stdOutOrig); | ||
} | ||
|
||
/** | ||
* Verify if the {@link GiantView} does what it has to do: Print the {@link GiantModel} to the | ||
* standard out stream, nothing more, nothing less. | ||
*/ | ||
@Test | ||
public void testDisplayGiant() { | ||
final GiantView view = new GiantView(); | ||
|
||
final GiantModel model = mock(GiantModel.class); | ||
view.displayGiant(model); | ||
|
||
verify(this.stdOutMock).println(model); | ||
verifyNoMoreInteractions(model, this.stdOutMock); | ||
|
||
} | ||
|
||
} |