-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Forward, Register, User Controller 테스트 작성
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
app/src/test/java/com/techcourse/controller/ForwardControllerTest.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,31 @@ | ||
package com.techcourse.controller; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
class ForwardControllerTest { | ||
|
||
@DisplayName("기본 index 페이지를 반환한다.") | ||
@Test | ||
void show() { | ||
// given | ||
final ForwardController forwardController = new ForwardController(); | ||
final HttpServletRequest request = mock(HttpServletRequest.class); | ||
final HttpServletResponse response = mock(HttpServletResponse.class); | ||
|
||
final ModelAndView expected = new ModelAndView(new JspView("/index.jsp")); | ||
|
||
// when | ||
final ModelAndView actual = forwardController.show(request, response); | ||
|
||
// then | ||
assertThat(actual).usingRecursiveComparison().isEqualTo(expected); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
package com.techcourse.controller; | ||
|
||
import static com.techcourse.repository.InMemoryUserRepository.findByAccount; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
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 org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
import webmvc.org.springframework.web.servlet.view.JspView; | ||
|
||
class RegisterControllerTest { | ||
|
||
@DisplayName("새로운 사용자를 올바르게 저장할 수 있다.") | ||
@Test | ||
void save() { | ||
// given | ||
final RegisterController registerController = new RegisterController(); | ||
final HttpServletRequest request = mock(HttpServletRequest.class); | ||
final HttpServletResponse response = mock(HttpServletResponse.class); | ||
|
||
final User dino = new User(2L, "dino", "good", "[email protected]"); | ||
|
||
when(request.getParameter("account")).thenReturn(dino.getAccount()); | ||
when(request.getParameter("password")).thenReturn("good"); | ||
when(request.getParameter("email")).thenReturn("[email protected]"); | ||
|
||
// when | ||
registerController.save(request, response); | ||
|
||
// then | ||
Assertions.assertAll( | ||
() -> assertThat(findByAccount("dino")).isPresent(), | ||
() -> assertThat(findByAccount("dino").get()).usingRecursiveComparison().isEqualTo(dino) | ||
); | ||
} | ||
|
||
@DisplayName("회원가입 페이지를 반환한다.") | ||
@Test | ||
void show() { | ||
// given | ||
final RegisterController registerController = new RegisterController(); | ||
final HttpServletRequest request = mock(HttpServletRequest.class); | ||
final HttpServletResponse response = mock(HttpServletResponse.class); | ||
|
||
final ModelAndView expected = new ModelAndView(new JspView("register.jsp")); | ||
|
||
// when | ||
final ModelAndView actual = registerController.show(request, response); | ||
|
||
// then | ||
assertThat(actual).usingRecursiveComparison().isEqualTo(expected); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/test/java/com/techcourse/controller/UserControllerTest.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,36 @@ | ||
package com.techcourse.controller; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
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 org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import webmvc.org.springframework.web.servlet.ModelAndView; | ||
|
||
class UserControllerTest { | ||
|
||
@DisplayName("account 값을 통해 메모리에 저장한 사용자의 정보를 파악할 수 있다.") | ||
@Test | ||
void show() { | ||
// given | ||
final UserController userController = new UserController(); | ||
final HttpServletRequest request = mock(HttpServletRequest.class); | ||
final HttpServletResponse response = mock(HttpServletResponse.class); | ||
|
||
final User user = new User(99L, "gr", "ay", "[email protected]"); | ||
InMemoryUserRepository.save(user); | ||
|
||
when(request.getParameter("account")).thenReturn(user.getAccount()); | ||
|
||
// when | ||
final ModelAndView modelAndView = userController.show(request, response); | ||
|
||
// then | ||
assertThat(modelAndView.getObject("user")).isEqualTo(user); | ||
} | ||
} |