공부 내용을 정리하고 앞으로의 학습에 이해를 돕기 위해 작성합니다.
어드민 서비스에서 제공할 예정인 주요 뷰와 각 스펙을 테스트로 어느 정도 미리 정의해 본다. 기능을 구현하면서 들어갈 디테일한 스펙들은 추후 별도의 이슈로 다룬다.
1. 엔드포인트 구성
어드민 서비스에서 사용할 주요 뷰는 다음과 같다.
- 메인 루트 뷰
- 게시글 관리 뷰
- 댓글 관리 뷰
- 회원 관리 뷰
- 어드민 회원 뷰
이러한 뷰들은 각각의 컨트롤러에 의해 관리되며, 이번 포스팅에서는 각 엔드포인트에 대한 기본적인 테스트 코드를 정의한다.
2. 컨트롤러 코드
2.1. 메인 루트 뷰 (MainController)
루트 URL에 대한 요청을 게시글 관리 페이지로 포워딩하는 컨트롤러이다.
package org.example.projectboardadmin.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class MainController {
@GetMapping("/")
public String root() {
return "forward:/management/articles";
}
}
- 이 컨트롤러는 어드민 페이지의 루트 경로 /를 처리한다.
- 사용자가 루트 URL을 요청할 때, 기본적으로 게시글 관리 페이지로 포워딩한다.
- 이렇게 함으로써, 어드민 페이지에 처음 접속하면 게시글 관리 페이지가 기본적으로 보이도록 설정하였다.
2.2. 게시글 관리 뷰 (ArticleManagementController)
게시글 관리 페이지를 처리하는 컨트롤러이다.
package org.example.projectboardadmin.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/management/articles")
@Controller
public class ArticleManagementController {
}
- 이 컨트롤러는 게시글 관리 페이지를 처리한다.
- URL 경로 /management/articles에 대응하며, 게시글과 관련된 관리 기능을 제공하는 뷰를 렌더링 하는 데 사용된다.
2.3. 댓글 관리 뷰 (ArticleCommentManagementController)
댓글 관리 페이지를 처리하는 컨트롤러이다.
package org.example.projectboardadmin.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/management/article-comments")
@Controller
public class ArticleCommentManagementController {
}
- 이 컨트롤러는 댓글 관리 페이지를 담당한다.
- URL 경로 /management/article-comments를 처리하며, 어드민이 댓글을 관리할 수 있는 기능을 제공하는 뷰를 렌더링 한다.
2.4. 회원 관리 뷰 (UserAccountManagementController)
회원 관리 페이지를 처리하는 컨트롤러이다.
package org.example.projectboardadmin.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/management/user-accounts")
@Controller
public class UserAccountManagementController {
}
- 이 컨트롤러는 회원 관리 페이지를 다룬다.
- URL 경로 /management/user-accounts를 처리하며, 사용자 계정과 관련된 관리 기능을 제공하는 뷰를 렌더링 한다.
2.5. 어드민 회원 뷰 (AdminUserAccountController)
어드민 회원 관리 페이지를 처리하는 컨트롤러이다.
package org.example.projectboardadmin.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/admin/members")
@Controller
public class AdminUserAccountController {
}
- 이 컨트롤러는 어드민 회원 관리 페이지를 처리한다.
- URL 경로 /admin/members를 처리하며, 어드민 사용자 계정과 관련된 기능을 제공하는 뷰를 렌더링한다.
3. 테스트 코드 정의
각 엔드포인트에 대한 기본적인 View 테스트를 정의한다. Spring의 MockMvc를 활용해 컨트롤러의 뷰 연결과 HTTP 상태 코드, 그리고 반환된 뷰 이름을 테스트한다.
3.1. 메인 루트 뷰 테스트
루트 URL에 대한 요청이 게시글 관리 페이지로 포워딩되는지 테스트한다.
package org.example.projectboardadmin.controller;
import org.example.projectboardadmin.config.SecurityConfig;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@DisplayName("View 루트 컨트롤러")
@Import(SecurityConfig.class)
@WebMvcTest(MainController.class)
class MainControllerTest {
private final MockMvc mvc;
public MainControllerTest(@Autowired MockMvc mvc) {
this.mvc = mvc;
}
@DisplayName("[view][GET] 루트 페이지 -> 게시글 관리 페이지 Forwarding")
@Test
void givenNothing_whenRequestingRootView_thenForwardsToArticleManagementView() throws Exception {
mvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("forward:/management/articles"))
.andExpect(forwardedUrl("/management/articles"));
}
}
- 루트 URL 요청 시, 요청이 게시글 관리 페이지로 정상적으로 포워딩되는지 확인한다.
- 이 테스트는 루트 경로 / 요청 시 상태 코드, 뷰 이름, 그리고 포워딩된 URL이 예상대로 동작하는지 검증한다.
3.2. 게시글 관리 뷰 테스트
게시글 관리 페이지가 정상적으로 호출되는지 테스트한다.
package org.example.projectboardadmin.controller;
import org.example.projectboardadmin.config.SecurityConfig;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@DisplayName("View 컨트롤러 - 게시글 관리")
@Import(SecurityConfig.class)
@WebMvcTest(ArticleManagementController.class)
class ArticleManagementControllerTest {
private final MockMvc mvc;
public ArticleManagementControllerTest(@Autowired MockMvc mvc) {
this.mvc = mvc;
}
@DisplayName("[view][GET] 게시글 관리 페이지 - 정상 호출")
@Test
void givenNothing_whenRequestingArticleManagementView_thenReturnsArticleManagementView() throws Exception {
mvc.perform(get("/management/articles"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
.andExpect(view().name("management/articles"));
}
}
- 게시글 관리 페이지가 정상적으로 호출되는지 확인한다.
- /management/articles 경로 요청 시 HTML 컨텐츠가 올바르게 반환되고, 올바른 뷰 이름을 가지고 있는지 테스트한다.
3.3. 댓글 관리 뷰 테스트
댓글 관리 페이지가 정상적으로 호출되는지 테스트한다.
package org.example.projectboardadmin.controller;
import org.example.projectboardadmin.config.SecurityConfig;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@DisplayName("View 컨트롤러 - 댓글 관리")
@Import(SecurityConfig.class)
@WebMvcTest(ArticleCommentManagementController.class)
class ArticleCommentManagementControllerTest {
private final MockMvc mvc;
public ArticleCommentManagementControllerTest(@Autowired MockMvc mvc) {
this.mvc = mvc;
}
@DisplayName("[view][GET] 댓글 관리 페이지 - 정상 호출")
@Test
void givenNothing_whenRequestingArticleCommentManagementView_thenReturnsArticleCommentManagementView() throws Exception {
mvc.perform(get("/management/article-comments"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
.andExpect(view().name("management/articleComments"));
}
}
- 댓글 관리 페이지가 정상적으로 호출되는지 확인한다.
- /management/article-comments 경로 요청 시, 상태 코드와 뷰 이름이 올바르게 반환되는지를 검증한다.
3.4. 회원 관리 뷰 테스트
회원 관리 페이지가 정상적으로 호출되는지 테스트한다.
package org.example.projectboardadmin.controller;
import org.example.projectboardadmin.config.SecurityConfig;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@DisplayName("View 컨트롤러 - 회원 관리")
@Import(SecurityConfig.class)
@WebMvcTest(UserAccountManagementController.class)
class UserAccountManagementControllerTest {
private final MockMvc mvc;
public UserAccountManagementControllerTest(@Autowired MockMvc mvc) {
this.mvc = mvc;
}
@DisplayName("[view][GET] 회원 관리 페이지 - 정상 호출")
@Test
void givenNothing_whenRequestingUserAccountManagementView_thenReturnsUserAccountManagementView() throws Exception {
mvc.perform(get("/management/user-accounts"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
.andExpect(view().name("management/userAccounts"));
}
}
- 회원 관리 페이지가 정상적으로 호출되는지 확인한다.
- /management/user-accounts 경로 요청 시, HTML 컨텐츠와 뷰 이름이 예상대로 반환되는지를 테스트한다.
3.5. 어드민 회원 뷰 테스트
어드민 회원 관리 페이지가 정상적으로 호출되는지 테스트한다.
package org.example.projectboardadmin.controller;
import org.example.projectboardadmin.config.SecurityConfig;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@DisplayName("View 컨트롤러 - 어드민 회원 관리")
@Import(SecurityConfig.class)
@WebMvcTest(AdminUserAccountController.class)
class AdminUserAccountControllerTest {
private final MockMvc mvc;
public AdminUserAccountControllerTest(@Autowired MockMvc mvc) {
this.mvc = mvc;
}
@DisplayName("[view][GET] 어드민 회원 관리 페이지 - 정상 호출")
@Test
void givenNothing_whenRequestingAdminUserAccountManagementView_thenReturnsAdminUserAccountManagementView() throws Exception {
mvc.perform(get("/admin/members"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
.andExpect(view().name("admin/members"));
}
}
- 어드민 회원 관리 페이지가 정상적으로 호출되는지 확인한다.
- /admin/members 경로 요청 시, 상태 코드, 컨텐츠 유형, 그리고 뷰 이름이 올바르게 반환되는지 테스트한다.
4. 뷰 파일 구성
이번 작업에서는 기본적인 뷰 파일을 생성하여 테스트를 통과시켰다. 각 뷰는 어드민 기능에서 제공할 주요 페이지들로 구성된다.
1. 뷰 파일 구조
2. 각 HTML 파일의 기본 구조
모든 HTML 파일은 다음과 같은 기본 구조를 가지고 있다.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
이번 작업에서는 어드민 페이지의 주요 뷰와 엔드포인트를 설정하고 기본적인 테스트를 완료했다. 이후 기능이 구현되면서 더 디테일한 요구 사항이 추가될 수 있고, 이러한 사항들은 추후 업데이트하여 반영할 예정이다. 이번 테스트 정의를 통해 기본적인 뷰 동작을 확인했으니, 이를 바탕으로 차후의 개발 과정에서 더욱 견고한 시스템을 구축할 수 있을 것이다.
이제 각 뷰에 필요한 실제 데이터와 기능을 추가하고, 사용자 경험을 개선하는 작업을 진행할 예정이다. 향후 발전된 기능과 디자인을 통해 더욱 완성도 높은 어드민 페이지를 만들어 갈 것이다.
'BackEnd > Project' 카테고리의 다른 글
[Admin] Ch02. 게시글 관리 페이지 만들기 (0) | 2024.08.19 |
---|---|
[Admin] Ch02. AdminLTE 기초 셋업 (0) | 2024.08.19 |
[Admin] Ch02. 데이터베이스 접근 로직 테스트 정의 (0) | 2024.08.18 |
[Admin] Ch02. 도메인 설계 (0) | 2024.08.18 |
[Admin] Ch02. 스프링부트 프로젝트 시작하기 (0) | 2024.08.17 |