Skip to content

Commit 9059d2b

Browse files
committed
Added proper tests for front-controller pattern
1 parent dbca06a commit 9059d2b

File tree

6 files changed

+263
-0
lines changed

6 files changed

+263
-0
lines changed

front-controller/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@
1515
<artifactId>junit</artifactId>
1616
<scope>test</scope>
1717
</dependency>
18+
<dependency>
19+
<groupId>org.mockito</groupId>
20+
<artifactId>mockito-core</artifactId>
21+
<scope>test</scope>
22+
</dependency>
1823
</dependencies>
1924
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.iluwatar.front.controller;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* Date: 12/13/15 - 1:35 PM
9+
*
10+
* @author Jeroen Meulemeester
11+
*/
12+
public class ApplicationExceptionTest {
13+
14+
@Test
15+
public void testCause() throws Exception {
16+
final Exception cause = new Exception();
17+
assertSame(cause, new ApplicationException(cause).getCause());
18+
}
19+
20+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.iluwatar.front.controller;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.Parameterized;
6+
import org.junit.runners.Parameterized.Parameters;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import static org.mockito.Mockito.verify;
12+
import static org.mockito.Mockito.verifyNoMoreInteractions;
13+
import static org.mockito.Mockito.verifyZeroInteractions;
14+
15+
/**
16+
* Date: 12/13/15 - 1:39 PM
17+
*
18+
* @author Jeroen Meulemeester
19+
*/
20+
@RunWith(Parameterized.class)
21+
public class CommandTest extends StdOutTest {
22+
23+
@Parameters
24+
public static List<Object[]> data() {
25+
final List<Object[]> parameters = new ArrayList<>();
26+
parameters.add(new Object[]{"Archer", "Displaying archers"});
27+
parameters.add(new Object[]{"Catapult", "Displaying catapults"});
28+
parameters.add(new Object[]{"NonExistentCommand", "Error 500"});
29+
return parameters;
30+
}
31+
32+
/**
33+
* The view that's been tested
34+
*/
35+
private final String request;
36+
37+
/**
38+
* The expected display message
39+
*/
40+
private final String displayMessage;
41+
42+
/**
43+
* Create a new instance of the {@link CommandTest} with the given view and expected message
44+
*
45+
* @param request The request that's been tested
46+
* @param displayMessage The expected display message
47+
*/
48+
public CommandTest(final String request, final String displayMessage) {
49+
this.displayMessage = displayMessage;
50+
this.request = request;
51+
}
52+
53+
@Test
54+
public void testDisplay() {
55+
final FrontController frontController = new FrontController();
56+
verifyZeroInteractions(getStdOutMock());
57+
frontController.handleRequest(request);
58+
verify(getStdOutMock()).println(displayMessage);
59+
verifyNoMoreInteractions(getStdOutMock());
60+
}
61+
62+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.iluwatar.front.controller;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.Parameterized;
6+
import org.junit.runners.Parameterized.Parameters;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import static org.mockito.Mockito.verify;
12+
import static org.mockito.Mockito.verifyNoMoreInteractions;
13+
import static org.mockito.Mockito.verifyZeroInteractions;
14+
15+
/**
16+
* Date: 12/13/15 - 1:39 PM
17+
*
18+
* @author Jeroen Meulemeester
19+
*/
20+
@RunWith(Parameterized.class)
21+
public class FrontControllerTest extends StdOutTest {
22+
23+
@Parameters
24+
public static List<Object[]> data() {
25+
final List<Object[]> parameters = new ArrayList<>();
26+
parameters.add(new Object[]{new ArcherCommand(), "Displaying archers"});
27+
parameters.add(new Object[]{new CatapultCommand(), "Displaying catapults"});
28+
parameters.add(new Object[]{new UnknownCommand(), "Error 500"});
29+
return parameters;
30+
}
31+
32+
/**
33+
* The view that's been tested
34+
*/
35+
private final Command command;
36+
37+
/**
38+
* The expected display message
39+
*/
40+
private final String displayMessage;
41+
42+
/**
43+
* Create a new instance of the {@link FrontControllerTest} with the given view and expected message
44+
*
45+
* @param command The command that's been tested
46+
* @param displayMessage The expected display message
47+
*/
48+
public FrontControllerTest(final Command command, final String displayMessage) {
49+
this.displayMessage = displayMessage;
50+
this.command = command;
51+
}
52+
53+
@Test
54+
public void testDisplay() {
55+
verifyZeroInteractions(getStdOutMock());
56+
this.command.process();
57+
verify(getStdOutMock()).println(displayMessage);
58+
verifyNoMoreInteractions(getStdOutMock());
59+
}
60+
61+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.iluwatar.front.controller;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
6+
import java.io.PrintStream;
7+
8+
import static org.mockito.Mockito.mock;
9+
10+
/**
11+
* Date: 12/10/15 - 8:37 PM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public abstract class StdOutTest {
16+
17+
/**
18+
* The mocked standard out {@link PrintStream}, required since the actions of the views don't have
19+
* any influence on any other accessible objects, except for writing to std-out using {@link
20+
* System#out}
21+
*/
22+
private final PrintStream stdOutMock = mock(PrintStream.class);
23+
24+
/**
25+
* Keep the original std-out so it can be restored after the test
26+
*/
27+
private final PrintStream stdOutOrig = System.out;
28+
29+
/**
30+
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
31+
*/
32+
@Before
33+
public void setUp() {
34+
System.setOut(this.stdOutMock);
35+
}
36+
37+
/**
38+
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
39+
*/
40+
@After
41+
public void tearDown() {
42+
System.setOut(this.stdOutOrig);
43+
}
44+
45+
/**
46+
* Get the mocked stdOut {@link PrintStream}
47+
*
48+
* @return The stdOut print stream mock, renewed before each test
49+
*/
50+
final PrintStream getStdOutMock() {
51+
return this.stdOutMock;
52+
}
53+
54+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.iluwatar.front.controller;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.Parameterized;
6+
import org.junit.runners.Parameterized.Parameters;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import static org.mockito.Mockito.verify;
12+
import static org.mockito.Mockito.verifyNoMoreInteractions;
13+
import static org.mockito.Mockito.verifyZeroInteractions;
14+
15+
/**
16+
* Date: 12/13/15 - 1:39 PM
17+
*
18+
* @author Jeroen Meulemeester
19+
*/
20+
@RunWith(Parameterized.class)
21+
public class ViewTest extends StdOutTest {
22+
23+
@Parameters
24+
public static List<Object[]> data() {
25+
final List<Object[]> parameters = new ArrayList<>();
26+
parameters.add(new Object[]{new ArcherView(), "Displaying archers"});
27+
parameters.add(new Object[]{new CatapultView(), "Displaying catapults"});
28+
parameters.add(new Object[]{new ErrorView(), "Error 500"});
29+
return parameters;
30+
}
31+
32+
/**
33+
* The view that's been tested
34+
*/
35+
private final View view;
36+
37+
/**
38+
* The expected display message
39+
*/
40+
private final String displayMessage;
41+
42+
/**
43+
* Create a new instance of the {@link ViewTest} with the given view and expected message
44+
*
45+
* @param view The view that's been tested
46+
* @param displayMessage The expected display message
47+
*/
48+
public ViewTest(final View view, final String displayMessage) {
49+
this.displayMessage = displayMessage;
50+
this.view = view;
51+
}
52+
53+
@Test
54+
public void testDisplay() {
55+
verifyZeroInteractions(getStdOutMock());
56+
this.view.display();
57+
verify(getStdOutMock()).println(displayMessage);
58+
verifyNoMoreInteractions(getStdOutMock());
59+
}
60+
61+
}

0 commit comments

Comments
 (0)