-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KL-64/test: create response advice test
- global response advice 테스트코드 추가
- Loading branch information
1 parent
4bce291
commit e65b6cd
Showing
1 changed file
with
105 additions
and
0 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
src/test/java/taco/klkl/global/response/GlobalResponseAdviceTest.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,105 @@ | ||
package taco.klkl.global.response; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.server.ServerHttpRequest; | ||
import org.springframework.http.server.ServletServerHttpResponse; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
|
||
class GlobalResponseAdviceTest { | ||
|
||
private GlobalResponseAdvice advice; | ||
|
||
@Mock | ||
private ServerHttpRequest request; | ||
|
||
@Mock | ||
private ServletServerHttpResponse response; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
advice = new GlobalResponseAdvice(); | ||
} | ||
|
||
@Test | ||
@DisplayName("supports 메소드가 항상 true를 반환하는지 확인") | ||
void testSupports() { | ||
assertTrue(advice.supports(null, null)); | ||
} | ||
|
||
@Test | ||
@DisplayName("성공 상태 코드(2xx)에 대해 GlobalResponse 객체를 반환하는지 확인") | ||
void testBeforeBodyWrite_WithSuccessStatus() { | ||
// Given | ||
Object body = new Object(); | ||
MockHttpServletResponse mockResponse = new MockHttpServletResponse(); | ||
mockResponse.setStatus(HttpStatus.OK.value()); | ||
when(response.getServletResponse()).thenReturn(mockResponse); | ||
|
||
// When | ||
Object result = advice.beforeBodyWrite(body, null, MediaType.APPLICATION_JSON, null, request, response); | ||
|
||
// Then | ||
assertTrue(result instanceof GlobalResponse); | ||
GlobalResponse globalResponse = (GlobalResponse)result; | ||
assertEquals("C000", globalResponse.code()); | ||
assertEquals(body, globalResponse.data()); | ||
} | ||
|
||
@Test | ||
@DisplayName("오류 상태 코드에 대해 원본 body를 그대로 반환하는지 확인") | ||
void testBeforeBodyWrite_WithErrorStatus() { | ||
// Given | ||
Object body = new Object(); | ||
MockHttpServletResponse mockResponse = new MockHttpServletResponse(); | ||
mockResponse.setStatus(HttpStatus.BAD_REQUEST.value()); | ||
when(response.getServletResponse()).thenReturn(mockResponse); | ||
|
||
// When | ||
Object result = advice.beforeBodyWrite(body, null, MediaType.APPLICATION_JSON, null, request, response); | ||
|
||
// Then | ||
assertEquals(body, result); | ||
} | ||
|
||
@Test | ||
@DisplayName("body가 String 타입일 때 원본 body를 그대로 반환하는지 확인") | ||
void testBeforeBodyWrite_WithStringBody() { | ||
// Given | ||
String body = "Test String"; | ||
MockHttpServletResponse mockResponse = new MockHttpServletResponse(); | ||
mockResponse.setStatus(HttpStatus.OK.value()); | ||
when(response.getServletResponse()).thenReturn(mockResponse); | ||
|
||
// When | ||
Object result = advice.beforeBodyWrite(body, null, MediaType.APPLICATION_JSON, null, request, response); | ||
|
||
// Then | ||
assertEquals(body, result); | ||
} | ||
|
||
@Test | ||
@DisplayName("HttpStatus가 null일 때 (즉, 유효하지 않은 상태 코드) 원본 body를 그대로 반환하는지 확인") | ||
void testBeforeBodyWrite_WithNullHttpStatus() { | ||
// Given | ||
Object body = new Object(); | ||
MockHttpServletResponse mockResponse = new MockHttpServletResponse(); | ||
mockResponse.setStatus(999); // Invalid status code | ||
when(response.getServletResponse()).thenReturn(mockResponse); | ||
|
||
// When | ||
Object result = advice.beforeBodyWrite(body, null, MediaType.APPLICATION_JSON, null, request, response); | ||
|
||
// Then | ||
assertEquals(body, result); | ||
} | ||
} |