package de.superx.servlet; import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import de.superx.common.SxUser; /** * @author hiber */ public class BaseServletTest { @Mock protected HttpServletRequest request; @Mock protected HttpServletResponse response; @Mock protected HttpSession session; @Before public void setupMocks() { this.request = mock(HttpServletRequest.class); this.response = mock(HttpServletResponse.class); this.session = mock(HttpSession.class); } @Test public void testMocks() { assertNotNull("mock object not null", this.request); assertNotNull("mock object not null", this.response); assertNotNull("mock object not null", this.session); } /** * Method to setup the {@code HttpSession} and {@code HttpServletRequest} to * simulate a proper session for testing. */ protected void setProperSession() { when(this.session.getAttribute("sessiontype")).thenReturn("superx"); when(this.session.getAttribute("user")).thenReturn(new SxUser("klenke")); when(this.request.getSession(false)).thenReturn(this.session); when(this.request.getSession(true)).thenReturn(this.session); when(this.request.getSession()).thenReturn(this.session); } /** * Method to setup up the {@code HttpServletRequest} to simulate multipart * content. This method sets the method and content type of the * {@code HttpServletRequest} * * @param setMultipartContent set multipart to true or false */ protected void setMultipartContent(boolean setMultipartContent) { if (setMultipartContent) { when(this.request.getMethod()).thenReturn("post"); when(this.request.getContentType()).thenReturn("multipart/"); } else { when(this.request.getMethod()).thenReturn("post"); when(this.request.getContentType()).thenReturn(null); } } }