Context and Servlet Initialization Parameters (#4656)
* Initial Commit * Add source files * Add readme.md * Add web.xml * Update pom.xml * Update pom.xml * Update pom.xml * Remove project folder * Update pom.xml
This commit is contained in:
committed by
KevinGilmore
parent
acf731377e
commit
ca1a1d70fa
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.servlets;
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user