Added tests for model-view-controller pattern

This commit is contained in:
Jeroen Meulemeester 2015-12-20 14:31:36 +01:00
parent a57a71b09c
commit 69c9374669
4 changed files with 225 additions and 0 deletions

View File

@ -14,5 +14,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -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);
}
}

View File

@ -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());
}
}
}

View File

@ -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);
}
}