Created
July 8, 2018 03:51
-
-
Save dfparker2002/bff29c472e5d492f8abb49dc62a4640b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src: https://github.com/alejandrogervasio/tutorials-3/blob/1ac81fb8589e8632c7738c5bc9773a8d63529d82/javax-servlets/src/test/java/com/baeldung/test/UserServletUnitTest.java | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import static org.assertj.core.api.Assertions.assertThat; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import static org.mockito.Mockito.atLeast; | |
import static org.mockito.Mockito.mock; | |
import static org.mockito.Mockito.verify; | |
import static org.mockito.Mockito.when; | |
public class UserServletUnitTest { | |
private static HttpServletRequest request; | |
private static HttpServletResponse response; | |
@BeforeClass | |
public static void setUpHttpServletRequestMockInstance() { | |
request = mock(HttpServletRequest.class); | |
} | |
@BeforeClass | |
public static void setUpHttpServletResponsetMockInstance() { | |
response = mock(HttpServletResponse.class); | |
} | |
@Test | |
public void givenHttpServletRequestMockInstance_whenCalledgetParameter_thenCalledAtLeastOnce() { | |
request.getParameter("name"); | |
verify(request, atLeast(1)).getParameter("name"); | |
} | |
@Test | |
public void givenHttpServletRequestMockInstance_whenCalledgetParameter_thenOneAssertion() { | |
when(request.getParameter("name")).thenReturn("username"); | |
assertThat(request.getParameter("name")).isEqualTo("username"); | |
} | |
@Test | |
public void givenHttpServletResponseMockInstance_whenCalledgetContentType_thenCalledAtLeastOnce() { | |
response.getContentType(); | |
verify(response, atLeast(1)).getContentType(); | |
} | |
@Test | |
public void givenHttpServletResponseMockInstance_whenCalledgetContentType_thenOneAssertion() { | |
when(response.getContentType()).thenReturn("text/html"); | |
assertThat(response.getContentType()).isEqualTo("text/html"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment