-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit tests for homepage and logout (#89)
* I have implemented the unit tests for the homepage servlet and logout servlet. * PR changes for testing have been implemented. * Test cases have been updated. * Test cases have been updated. * PR Test cases have been edited again. * PR Test cases have been edited again.
- Loading branch information
1 parent
9a733e3
commit 75d241c
Showing
4 changed files
with
109 additions
and
6 deletions.
There are no files selected for viewing
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
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
49 changes: 49 additions & 0 deletions
49
src/test/java/net/novucs/esd/test/controller/TestHomepageServlet.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package net.novucs.esd.test.controller; | ||
|
||
import static junit.framework.TestCase.assertTrue; | ||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.RequestDispatcher; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import net.novucs.esd.controllers.HomepageServlet; | ||
import org.junit.Test; | ||
import org.mockito.stubbing.Answer; | ||
|
||
public class TestHomepageServlet { | ||
|
||
@Test | ||
public void testRequestGetsMapAttribute() | ||
throws ServletException, IOException { | ||
// Given | ||
HomepageServlet servlet = new HomepageServlet(); | ||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
HttpServletResponse response = mock(HttpServletResponse.class); | ||
|
||
// When | ||
when(request.getRequestDispatcher("/homepage.jsp")).thenAnswer( | ||
(Answer<RequestDispatcher>) invocation -> mock(RequestDispatcher.class)); | ||
servlet.doGet(request, response); | ||
|
||
// Assert | ||
verify(request).getRequestDispatcher(eq("/homepage.jsp")); | ||
} | ||
|
||
@Test | ||
public void testHomepageInServletInfo() { | ||
// Given | ||
HomepageServlet servlet = new HomepageServlet(); | ||
|
||
// When | ||
String servletInfo = servlet.getServletInfo(); | ||
|
||
// Assert | ||
assertTrue("Homepage servlet info must contain the key word Homepage", | ||
servletInfo.contains("Homepage")); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/test/java/net/novucs/esd/test/controller/TestLogoutServlet.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package net.novucs.esd.test.controller; | ||
|
||
import static junit.framework.TestCase.assertTrue; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.RequestDispatcher; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
import net.novucs.esd.controllers.LogoutServlet; | ||
import org.junit.Test; | ||
|
||
public class TestLogoutServlet { | ||
|
||
@Test | ||
public void testRequestGetsMapAttribute() | ||
throws ServletException, IOException { | ||
// Given | ||
LogoutServlet servlet = new LogoutServlet(); | ||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
HttpServletResponse response = mock(HttpServletResponse.class); | ||
|
||
// When | ||
when(request.getSession(true)).thenReturn(mock(HttpSession.class)); | ||
when(request.getRequestDispatcher(any())).thenReturn(mock(RequestDispatcher.class)); | ||
servlet.doGet(request, response); | ||
|
||
// Assert | ||
verify(response, times(1)).sendRedirect(eq("login")); | ||
} | ||
|
||
@Test | ||
public void testLogoutInServletInfo() { | ||
// Given | ||
LogoutServlet servlet = new LogoutServlet(); | ||
|
||
// When | ||
String servletInfo = servlet.getServletInfo(); | ||
|
||
// Assert | ||
assertTrue("Logout servlet info must contain the key word Logout", | ||
servletInfo.contains("Logout")); | ||
} | ||
} |