Skip to content

Commit

Permalink
test: 컨트롤러 테스트 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
thdwoqor committed Sep 16, 2023
1 parent 5a00194 commit 2f2cefc
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 0 deletions.
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");
}
}
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");
}
}
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:/");
}
}
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();
}
}
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");
}
}
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);
}
}

0 comments on commit 2f2cefc

Please sign in to comment.