use of codeu.model.store.basic.UserStore in project CodeU-Spring-2018 by jwang281.
the class RegisterServletTest method testDoPost_NewUser_2.
@Test
public void testDoPost_NewUser_2() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("username")).thenReturn("test 2 username");
UserStore mockUserStore = Mockito.mock(UserStore.class);
Mockito.when(mockUserStore.isUserRegistered("test 2 username")).thenReturn(false);
registerServlet.setUserStore(mockUserStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
registerServlet.doPost(mockRequest, mockResponse);
ArgumentCaptor<User> userArgumentCaptor = ArgumentCaptor.forClass(User.class);
Mockito.verify(mockUserStore).addUser(userArgumentCaptor.capture());
Assert.assertEquals(userArgumentCaptor.getValue().getName(), "test 2 username");
Mockito.verify(mockUserStore, Mockito.atLeastOnce()).addUser(Mockito.any(User.class));
Mockito.verify(mockResponse).sendRedirect("/login");
}
use of codeu.model.store.basic.UserStore in project CodeU-Spring-2018 by jwang281.
the class RegisterServletTest method testDoPost_ExistingUser.
@Test
public void testDoPost_ExistingUser() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("username")).thenReturn("test username");
UserStore mockUserStore = Mockito.mock(UserStore.class);
Mockito.when(mockUserStore.isUserRegistered("test username")).thenReturn(true);
registerServlet.setUserStore(mockUserStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
registerServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockUserStore, Mockito.never()).addUser(Mockito.any(User.class));
Mockito.verify(mockRequest).setAttribute("error", "That username is already taken.");
Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
Aggregations