제목, 태그, 카테고리로 검색

모든 글
약 7분 분량 프로젝트/에듀밋

에지 케이스 테스트에서 발견한 버그들

목차

정상 동작하는 테스트 코드는 생략하고, 실패 사례와 그 해결 과정만 정리했습니다.


게시글

1. 제목이 없는 게시글 등록

정상 상태

게시글 등록 시 제목은 필수값이에요. 제목이 비어 있으면 등록이 거부되어야 하고요.

문제

제목 없이 게시글을 등록하는 테스트를 작성했는데, 예외 없이 정상 등록됐어요.

분석

검증 로직의 위치를 고민했어요.

레이어드 아키텍처의 책임 분리 원칙에 따르면:

  • Controller: 요청 형식 검증 (@Valid, 타입 체크)
  • Service: 비즈니스 규칙 검증 (도메인 규칙, 상태 검증)
  • Repository: 순수 데이터 접근만

제목 필수 여부는 비즈니스 규칙이므로 Service에 넣는 것이 맞다고 판단했어요.

해결

Service에 제목 검증 로직을 추가했어요.


2. 좋아요 토글 - 정수 오버플로우

문제

좋아요 수가 Integer.MAX_VALUE일 때 한 번 더 증가시키면 오버플로우가 발생했어요.

해결

좋아요 증가 메서드에 오버플로우 방지 로직을 추가했어요.


3. 매우 긴 제목의 게시글 등록

문제

제목 길이 제한 없이 등록을 시도하면, DB 컬럼 제한에 걸려 DataIntegrityViolationException이 발생했어요.

해결

Service의 제목 검증 메서드에 최대 길이 체크를 추가해서, DB에 도달하기 전에 예외를 던지도록 했어요.


4. 논리적 삭제(Soft Delete) 기능이 동작하지 않음

정상 상태

게시글을 삭제하면 deleted_at 컬럼에 삭제 시각이 기록되고, 조회 시 deleted_at IS NULL 조건으로 삭제된 게시글이 제외되어야 해요.

문제

삭제 처리를 했는데, 삭제된 게시글이 여전히 조회되고 있었어요.

분석

원인이 2가지였어요.

원인 1: 조회 쿼리에 deleted_at IS NULL 조건이 빠져 있었어요.

원인 2: 순수 도메인 엔티티에 deletedAt 필드가 없었어요.

해결

  1. 도메인 엔티티에 deletedAt 필드 추가
  2. 조회 쿼리에 WHERE deleted_at IS NULL 조건 추가


5. 카테고리별 게시글 조회 실패

문제

특정 카테고리의 게시글만 조회하려 했는데, 카테고리와 무관하게 전체 게시글이 반환됐어요.

원인

QueryDSL의 WHERE 절에 카테고리 조건이 빠져 있었어요.

해결

WHERE 절에 카테고리 필터 조건을 추가했어요.


6. 좋아요/싫어요 토글에서 싫어요만 작동하지 않음

문제

UPDATE 문이 실행되지 않고, 새로운 엔티티로 INSERT하고 있었어요.

해결

좋아요와 싫어요를 하나의 공통 메서드로 통합해서 동일한 로직을 사용하도록 했어요.


7. 빈 게시글의 계층형 댓글 목록 조회 시 NPE

문제

PageResponseDTO 생성자에서 total <= 0일 때 early return하면 모든 필드가 초기화되지 않아 NPE가 발생했어요.

해결

total <= 0일 때도 빈 결과에 대한 기본값을 설정한 뒤 return하도록 수정했어요.


8. 게시글 타입 변경이 반영되지 않음

문제

게시글 타입을 변경하는 API를 호출했는데, 타입이 바뀌지 않았어요.

원인

change() 메서드가 제목과 내용만 변경하고, boardType은 변경하지 않고 있었거든요.

해결

change() 메서드에 boardType 변경 로직을 추가했어요.


9. 존재하지 않는 카테고리에 게시글 등록 가능

문제

존재하지 않는 카테고리 ID를 넣어도 게시글이 등록됐어요.

원인

Service에서 카테고리 존재 여부를 검증하는 로직이 없었어요.

해결

게시글 등록 전에 카테고리 존재 여부를 확인하고, 없으면 예외를 던지도록 했어요.


10. 유효하지 않은 페이지 번호 처리

문제

page=0이나 page=-1을 넣어도 쿼리가 그대로 실행되어 예측할 수 없는 결과가 나왔어요.

해결

PageRequestDTO에 기본값과 범위 제한을 추가했어요.


댓글

1. 내용이 없는 댓글 등록 가능

문제

빈 문자열로 댓글을 등록할 수 있었어요.

해결

Service에서 빈 내용과 최대 길이를 함께 검증하도록 했어요.


2. 다른 게시글의 댓글에 대댓글 등록

문제

다른 게시글의 댓글 ID를 부모 댓글로 지정해도 대댓글이 등록됐어요.

해결

대댓글 등록 시, 부모 댓글이 현재 게시글에 속하는지 검증하는 로직을 추가했어요.


3. 매우 긴 내용의 댓글 등록 시 예외 타입 불일치

문제

댓글 최대 길이 초과 시 IllegalArgumentException을 던지도록 구현했는데, 테스트에서 InvalidDataAccessApiUsageException이 발생했어요.

원인

Spring Data JPA의 Repository 프록시가 IllegalArgumentExceptionInvalidDataAccessApiUsageException으로 자동 변환하고 있었어요.

해결

테스트 코드의 기대 예외 타입을 InvalidDataAccessApiUsageException으로 변경했어요.

Only failed test cases and their resolution processes are documented here; passing tests are omitted.


Posts

1. Registering a Post Without a Title

Normal Behavior

Title is required for post registration. Empty titles should be rejected.

Problem

A test registering a post without a title succeeded without any exception.

Analysis

Considered where to place validation logic.

Following the layered architecture’s separation of concerns:

  • Controller: Request format validation (@Valid, type checking)
  • Service: Business rule validation (domain rules, state verification)
  • Repository: Pure data access only

Title requirement is a business rule, so Service was the appropriate location.

Fix

Added title validation logic to the Service.


2. Like Toggle - Integer Overflow

Problem

When the like count was at Integer.MAX_VALUE, incrementing once more caused an overflow.

Fix

Added overflow prevention logic to the like increment method.


3. Registering a Post with an Extremely Long Title

Problem

Attempting to register without a title length limit triggered DataIntegrityViolationException from the DB column constraint.

Fix

Added maximum length validation to the Service’s title validation method, throwing an exception before reaching the DB.


4. Soft Delete Not Working

Normal Behavior

Deleting a post should record the deletion time in the deleted_at column, and queries should exclude deleted posts with deleted_at IS NULL.

Problem

After deletion, deleted posts were still appearing in queries.

Analysis

Two causes were identified:

Cause 1: The query was missing the deleted_at IS NULL condition.

Cause 2: The pure domain entity was missing the deletedAt field.

Fix

  1. Added deletedAt field to domain entity
  2. Added WHERE deleted_at IS NULL condition to queries


5. Category-Filtered Post Query Failure

Problem

Attempting to query posts by specific category returned all posts regardless of category.

Cause

The QueryDSL WHERE clause was missing the category filter condition.

Fix

Added category filter condition to the WHERE clause.


6. Like/Dislike Toggle - Only Dislike Broken

Problem

Instead of UPDATE, new entities were being INSERTed.

Fix

Unified like and dislike into a single shared method using identical logic.


7. NPE When Querying Hierarchical Comments on Empty Post

Problem

In PageResponseDTO constructor, early return when total <= 0 left all fields uninitialized, causing NPE.

Fix

Modified to set default values for empty results before returning when total <= 0.


8. Post Type Change Not Applied

Problem

After calling the API to change post type, the type remained unchanged.

Cause

The change() method only updated title and content, ignoring boardType.

Fix

Added boardType update logic to the change() method.


9. Post Registration with Non-Existent Category

Problem

Posts could be registered with non-existent category IDs.

Cause

The Service lacked category existence verification logic.

Fix

Added category existence check before post registration, throwing an exception if not found.


10. Invalid Page Number Handling

Problem

Providing page=0 or page=-1 executed the query as-is, producing unpredictable results.

Fix

Added default values and range limits to PageRequestDTO.


Comments

1. Registering Empty Comments

Problem

Comments could be registered with empty strings.

Fix

Added validation for both empty content and maximum length in the Service.


2. Replying to Comments from Different Posts

Problem

Reply comments could be created using parent comment IDs from different posts.

Fix

Added validation to verify the parent comment belongs to the current post during reply registration.


3. Exception Type Mismatch for Long Comment Content

Problem

Implemented IllegalArgumentException for exceeding maximum comment length, but tests received InvalidDataAccessApiUsageException.

Cause

Spring Data JPA’s Repository proxy was automatically converting IllegalArgumentException to InvalidDataAccessApiUsageException.

Fix

Changed the expected exception type in the test code to InvalidDataAccessApiUsageException.

Author
작성자 @범수

오늘의 노력이 내일의 전문성을 만든다고 믿습니다.

댓글