Added proper tests for front-controller pattern
This commit is contained in:
parent
dbca06a9e7
commit
9059d2b96c
@ -15,5 +15,10 @@
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -0,0 +1,20 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Date: 12/13/15 - 1:35 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class ApplicationExceptionTest {
|
||||
|
||||
@Test
|
||||
public void testCause() throws Exception {
|
||||
final Exception cause = new Exception();
|
||||
assertSame(cause, new ApplicationException(cause).getCause());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
/**
|
||||
* Date: 12/13/15 - 1:39 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class CommandTest extends StdOutTest {
|
||||
|
||||
@Parameters
|
||||
public static List<Object[]> data() {
|
||||
final List<Object[]> parameters = new ArrayList<>();
|
||||
parameters.add(new Object[]{"Archer", "Displaying archers"});
|
||||
parameters.add(new Object[]{"Catapult", "Displaying catapults"});
|
||||
parameters.add(new Object[]{"NonExistentCommand", "Error 500"});
|
||||
return parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* The view that's been tested
|
||||
*/
|
||||
private final String request;
|
||||
|
||||
/**
|
||||
* The expected display message
|
||||
*/
|
||||
private final String displayMessage;
|
||||
|
||||
/**
|
||||
* Create a new instance of the {@link CommandTest} with the given view and expected message
|
||||
*
|
||||
* @param request The request that's been tested
|
||||
* @param displayMessage The expected display message
|
||||
*/
|
||||
public CommandTest(final String request, final String displayMessage) {
|
||||
this.displayMessage = displayMessage;
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisplay() {
|
||||
final FrontController frontController = new FrontController();
|
||||
verifyZeroInteractions(getStdOutMock());
|
||||
frontController.handleRequest(request);
|
||||
verify(getStdOutMock()).println(displayMessage);
|
||||
verifyNoMoreInteractions(getStdOutMock());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
/**
|
||||
* Date: 12/13/15 - 1:39 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class FrontControllerTest extends StdOutTest {
|
||||
|
||||
@Parameters
|
||||
public static List<Object[]> data() {
|
||||
final List<Object[]> parameters = new ArrayList<>();
|
||||
parameters.add(new Object[]{new ArcherCommand(), "Displaying archers"});
|
||||
parameters.add(new Object[]{new CatapultCommand(), "Displaying catapults"});
|
||||
parameters.add(new Object[]{new UnknownCommand(), "Error 500"});
|
||||
return parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* The view that's been tested
|
||||
*/
|
||||
private final Command command;
|
||||
|
||||
/**
|
||||
* The expected display message
|
||||
*/
|
||||
private final String displayMessage;
|
||||
|
||||
/**
|
||||
* Create a new instance of the {@link FrontControllerTest} with the given view and expected message
|
||||
*
|
||||
* @param command The command that's been tested
|
||||
* @param displayMessage The expected display message
|
||||
*/
|
||||
public FrontControllerTest(final Command command, final String displayMessage) {
|
||||
this.displayMessage = displayMessage;
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisplay() {
|
||||
verifyZeroInteractions(getStdOutMock());
|
||||
this.command.process();
|
||||
verify(getStdOutMock()).println(displayMessage);
|
||||
verifyNoMoreInteractions(getStdOutMock());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Date: 12/10/15 - 8:37 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public abstract class StdOutTest {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mocked stdOut {@link PrintStream}
|
||||
*
|
||||
* @return The stdOut print stream mock, renewed before each test
|
||||
*/
|
||||
final PrintStream getStdOutMock() {
|
||||
return this.stdOutMock;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.iluwatar.front.controller;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
/**
|
||||
* Date: 12/13/15 - 1:39 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class ViewTest extends StdOutTest {
|
||||
|
||||
@Parameters
|
||||
public static List<Object[]> data() {
|
||||
final List<Object[]> parameters = new ArrayList<>();
|
||||
parameters.add(new Object[]{new ArcherView(), "Displaying archers"});
|
||||
parameters.add(new Object[]{new CatapultView(), "Displaying catapults"});
|
||||
parameters.add(new Object[]{new ErrorView(), "Error 500"});
|
||||
return parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* The view that's been tested
|
||||
*/
|
||||
private final View view;
|
||||
|
||||
/**
|
||||
* The expected display message
|
||||
*/
|
||||
private final String displayMessage;
|
||||
|
||||
/**
|
||||
* Create a new instance of the {@link ViewTest} with the given view and expected message
|
||||
*
|
||||
* @param view The view that's been tested
|
||||
* @param displayMessage The expected display message
|
||||
*/
|
||||
public ViewTest(final View view, final String displayMessage) {
|
||||
this.displayMessage = displayMessage;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisplay() {
|
||||
verifyZeroInteractions(getStdOutMock());
|
||||
this.view.display();
|
||||
verify(getStdOutMock()).println(displayMessage);
|
||||
verifyNoMoreInteractions(getStdOutMock());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user