@Test public void doPost_shouldNotAddPlayersToSession_whenSessionIsNotNewAndServletPathIsNotReset() throws ServletException, IOException { // Test gameServlet.doPost(request, response); // Verify verify(session, never()).setAttribute(eq(KEY_HUMAN_PLAYER), any(Player.class)); verify(session, never()).setAttribute(eq(KEY_ROBOT1), any(RobotPlayer.class)); verify(session, never()).setAttribute(eq(KEY_ROBOT2), any(RobotPlayer.class)); verify(session, never()).setAttribute(eq(KEY_ROBOT3), any(RobotPlayer.class)); }
@Test public void doPost_shouldInvokePlayWithCorrectParameters_whenServletPathIsPlayHumanVsRobot() throws ServletException, IOException { // Setup when(request.getServletPath()).thenReturn(PATH_HUMAN_VS_ROBOT); when(session.getAttribute(KEY_HUMAN_PLAYER)).thenReturn(humanPlayer); when(session.getAttribute(KEY_ROBOT3)).thenReturn(robot3); // Test gameServlet.doPost(request, response); // Verify verify(game).play(humanPlayer, robot3); }
@Test public void doPost_shouldInvokePlayWithCorrectParameters_whenServletPathIsPlayRobotVsRobot() throws ServletException, IOException { // Setup when(request.getServletPath()).thenReturn(PATH_ROBOT_VS_ROBOT); when(session.getAttribute(KEY_ROBOT1)).thenReturn(robot1); when(session.getAttribute(KEY_ROBOT2)).thenReturn(robot2); // Test gameServlet.doPost(request, response); // Verify verify(game).play(robot1, robot2); }
@Test public void doPost_shouldAddPlayersToSession_whenServletPathIsReset() throws ServletException, IOException { // Setup when(request.getServletPath()).thenReturn(PATH_RESET); // Test gameServlet.doPost(request, response); // Verify verify(session).setAttribute(eq(KEY_HUMAN_PLAYER), any(Player.class)); verify(session).setAttribute(eq(KEY_ROBOT1), any(RobotPlayer.class)); verify(session).setAttribute(eq(KEY_ROBOT2), any(RobotPlayer.class)); verify(session).setAttribute(eq(KEY_ROBOT3), any(RobotPlayer.class)); }
@Test public void doPost_shouldPopulateResponseWithCorrectData_whenServletPathIsPlayHumanVsRobot() throws ServletException, IOException, JSONException { // Setup Gesture expectedHumanGesture = Gesture.PAPER; Gesture expectedRobot3Gesture = Gesture.ROCK; int expectedHumanScore = 5; int expectedRobot3Score = 10; Result expectedResult = Result.PLAYER1; when(request.getServletPath()).thenReturn(PATH_HUMAN_VS_ROBOT); when(session.getAttribute(KEY_HUMAN_PLAYER)).thenReturn(humanPlayer); when(session.getAttribute(KEY_ROBOT3)).thenReturn(robot3); when(humanPlayer.getGesture()).thenReturn(expectedHumanGesture); when(robot3.getGesture()).thenReturn(expectedRobot3Gesture); when(humanPlayer.getScore()).thenReturn(expectedHumanScore); when(robot3.getScore()).thenReturn(expectedRobot3Score); when(game.play(humanPlayer, robot3)).thenReturn(expectedResult); // Test gameServlet.doPost(request, response); // Verify verify(writer).print(captorJson.capture()); assertEquals( "Response is not as expected", expectedHumanGesture, captorJson.getValue().get(KEY_PLAYER1_GESTURE)); assertEquals( "Response is not as expected", expectedRobot3Gesture, captorJson.getValue().get(KEY_PLAYER2_GESTURE)); assertEquals( "Response is not as expected", expectedHumanScore, captorJson.getValue().get(KEY_PLAYER1_SCORE)); assertEquals( "Response is not as expected", expectedRobot3Score, captorJson.getValue().get(KEY_PLAYER2_SCORE)); assertEquals( "Response is not as expected", expectedResult, captorJson.getValue().get(KEY_RESULT)); }