package edu.pdx.cs410J.apptbookweb; import org.junit.Test; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.mockito.Mockito.*; /** * A unit test for the {@link AppointmentBookServlet}. It uses mockito to * provide mock http requests and responses. */ public class AppointmentBookServletTest { @Test public void initiallyServletContainsNoKeyValueMappings() throws ServletException, IOException { AppointmentBookServlet servlet = new AppointmentBookServlet(); HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); PrintWriter pw = mock(PrintWriter.class); when(response.getWriter()).thenReturn(pw); servlet.doGet(request, response); int expectedMappings = 0; verify(pw).println(Messages.getMappingCount(expectedMappings)); verify(response).setStatus(HttpServletResponse.SC_OK); } @Test public void addOneMapping() throws ServletException, IOException { AppointmentBookServlet servlet = new AppointmentBookServlet(); String testKey = "TEST KEY"; String testValue = "TEST VALUE"; HttpServletRequest request = mock(HttpServletRequest.class); when(request.getParameter("key")).thenReturn(testKey); when(request.getParameter("value")).thenReturn(testValue); HttpServletResponse response = mock(HttpServletResponse.class); PrintWriter pw = mock(PrintWriter.class); when(response.getWriter()).thenReturn(pw); servlet.doPost(request, response); verify(pw).println(Messages.mappedKeyValue(testKey, testValue)); verify(response).setStatus(HttpServletResponse.SC_OK); assertThat(servlet.getValueForKey(testKey), equalTo(testValue)); } }