-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
app/src/test/java/com/techcourse/controller/LoginControllerTest.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,63 @@ | ||
package com.techcourse.controller; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.techcourse.domain.User; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.servlet.http.HttpSession; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class LoginControllerTest { | ||
|
||
@Test | ||
void 로그인_성공_테스트() throws Exception { | ||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
HttpSession session = mock(HttpSession.class); | ||
when(request.getParameter("account")).thenReturn("gugu"); | ||
when(request.getParameter("password")).thenReturn("password"); | ||
when(request.getSession()).thenReturn(session); | ||
|
||
LoginController loginController = new LoginController(); | ||
String execute = loginController.execute(request, response); | ||
|
||
Assertions.assertThat(execute).isEqualTo("redirect:/index.jsp"); | ||
} | ||
|
||
@Test | ||
void 로그인_실패_테스트() throws Exception { | ||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
HttpSession session = mock(HttpSession.class); | ||
when(request.getParameter("account")).thenReturn("power"); | ||
when(request.getParameter("password")).thenReturn("password"); | ||
when(request.getSession()).thenReturn(session); | ||
|
||
LoginController loginController = new LoginController(); | ||
String execute = loginController.execute(request, response); | ||
|
||
Assertions.assertThat(execute).isEqualTo("redirect:/401.jsp"); | ||
} | ||
|
||
@Test | ||
void 세션_존재_할때_로그인_테스트() throws Exception { | ||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
final var session = mock(HttpSession.class); | ||
|
||
when(session.getAttribute("user")).thenReturn(new User("power","power","[email protected]")); | ||
when(request.getSession()).thenReturn(session); | ||
|
||
LoginController loginController = new LoginController(); | ||
String execute = loginController.execute(request, response); | ||
|
||
Assertions.assertThat(execute).isEqualTo("redirect:/index.jsp"); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
app/src/test/java/com/techcourse/controller/LoginViewControllerTest.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,47 @@ | ||
package com.techcourse.controller; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.techcourse.domain.User; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.servlet.http.HttpSession; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class LoginViewControllerTest { | ||
|
||
@Test | ||
void 세션_존재_할때_로그인_테스트() throws Exception { | ||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
final var session = mock(HttpSession.class); | ||
|
||
when(session.getAttribute("user")).thenReturn(new User("power","power","[email protected]")); | ||
when(request.getSession()).thenReturn(session); | ||
|
||
LoginViewController loginViewController = new LoginViewController(); | ||
String execute = loginViewController.execute(request, response); | ||
|
||
Assertions.assertThat(execute).isEqualTo("redirect:/index.jsp"); | ||
} | ||
|
||
@Test | ||
void 세션_존재_하지_않을때_로그인_테스트() throws Exception { | ||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
final var session = mock(HttpSession.class); | ||
|
||
when(request.getSession()).thenReturn(session); | ||
|
||
LoginViewController loginViewController = new LoginViewController(); | ||
String execute = loginViewController.execute(request, response); | ||
|
||
Assertions.assertThat(execute).isEqualTo("/login.jsp"); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/test/java/com/techcourse/controller/LogoutControllerTest.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,32 @@ | ||
package com.techcourse.controller; | ||
|
||
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.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class LogoutControllerTest { | ||
|
||
|
||
@Test | ||
void 로그인_아웃_테스트() throws Exception { | ||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
final var session = mock(HttpSession.class); | ||
|
||
when(request.getSession()).thenReturn(session); | ||
|
||
LogoutController logoutController = new LogoutController(); | ||
String execute = logoutController.execute(request, response); | ||
|
||
Assertions.assertThat(execute).isEqualTo("redirect:/"); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
app/src/test/java/com/techcourse/controller/RegisterControllerTest.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,38 @@ | ||
package com.techcourse.controller; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.techcourse.domain.User; | ||
import com.techcourse.repository.InMemoryUserRepository; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.servlet.http.HttpSession; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class RegisterControllerTest { | ||
|
||
@Test | ||
void 회원가입_테스트() throws Exception { | ||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
HttpSession session = mock(HttpSession.class); | ||
|
||
when(request.getParameter("account")).thenReturn("power"); | ||
when(request.getParameter("password")).thenReturn("password"); | ||
when(request.getParameter("email")).thenReturn("[email protected]"); | ||
when(request.getSession()).thenReturn(session); | ||
|
||
RegisterController registerController = new RegisterController(); | ||
String execute = registerController.execute(request, response); | ||
|
||
Assertions.assertThat(execute).isEqualTo("redirect:/index.jsp"); | ||
User user = InMemoryUserRepository.findByAccount("power").get(); | ||
Assertions.assertThat(user.checkPassword("password")).isTrue(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/src/test/java/com/techcourse/controller/RegisterViewControllerTest.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,30 @@ | ||
package com.techcourse.controller; | ||
|
||
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.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class RegisterViewControllerTest { | ||
|
||
@Test | ||
void 회원가입_페이지_출력_테스트() throws Exception { | ||
final var request = mock(HttpServletRequest.class); | ||
final var response = mock(HttpServletResponse.class); | ||
HttpSession session = mock(HttpSession.class); | ||
when(request.getSession()).thenReturn(session); | ||
|
||
RegisterViewController registerViewController = new RegisterViewController(); | ||
String execute = registerViewController.execute(request, response); | ||
|
||
Assertions.assertThat(execute).isEqualTo("/register.jsp"); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/test/java/com/techcourse/repository/InMemoryUserRepositoryTest.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,22 @@ | ||
package com.techcourse.repository; | ||
|
||
import com.techcourse.domain.User; | ||
import java.util.Optional; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class InMemoryUserRepositoryTest { | ||
|
||
@Test | ||
void 유저_저장_테스트() { | ||
User user = new User("power", "password", "[email protected]"); | ||
InMemoryUserRepository.save(user); | ||
|
||
Optional<User> power = InMemoryUserRepository.findByAccount("power"); | ||
Assertions.assertThat(power.get().getId()).isEqualTo(2L); | ||
} | ||
} |