Skip to content

Commit

Permalink
test: Forward, Register, User Controller 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
jjongwa committed Sep 25, 2023
1 parent 2081f7f commit 57d26be
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
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);
}
}
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);
}
}
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);
}
}

0 comments on commit 57d26be

Please sign in to comment.