Loading...
Spring Framework Reference Documentation 7.0.2의 Async Requests의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
이 섹션은 비동기 request 처리를 test하기 위해 MockMvc를 단독으로 사용하는 방법을 보여줍니다.
WebTestClient를 통해 MockMvc를 사용하는 경우,
비동기 request가 동작하도록 하기 위해 특별히 할 일은 없습니다. WebTestClient가 이 섹션에서 설명하는 내용을 자동으로 수행하기 때문입니다.
Spring MVC에서 지원되는 Servlet 비동기 request는 Servlet 컨테이너 thread를 빠져나와 애플리케이션이 response를 비동기적으로 계산할 수 있도록 한 다음, 그 후 Servlet 컨테이너 thread에서 처리를 완료하기 위해 async dispatch를 수행하는 방식으로 동작합니다.
Spring MVC Test에서 async request는 먼저 생성된 async value를 assert한 다음,
수동으로 async dispatch를 수행하고 마지막으로 response를 검증함으로써 test할 수 있습니다.
아래는 DeferredResult, Callable 또는 Reactor Mono와 같은 reactive type을 반환하는 controller method에 대한 예제 test입니다:
1// static import of MockMvcRequestBuilders.* and MockMvcResultMatchers.* 2 3@Test 4void test() throws Exception { 5 MvcResult mvcResult = this.mockMvc.perform(get("/path")) 6 .andExpect(status().isOk()) // (1) 7 .andExpect(request().asyncStarted()) // (2) 8 .andExpect(request().asyncResult("body")) // (3) 9 .andReturn(); 10 11 this.mockMvc.perform(asyncDispatch(mvcResult)) // (4) 12 .andExpect(status().isOk()) // (5) 13 .andExpect(content().string("body")); 14}
| 1 | response status가 여전히 변경되지 않았는지 확인 |
| 2 | async processing이 시작되었어야 함 |
| 3 | async result를 대기하고 assert |
| 4 | (실행 중인 컨테이너가 없으므로) 수동으로 ASYNC dispatch 수행 |
| 5 | 최종 response 검증 |
1@Test 2fun test() { 3 var mvcResult = mockMvc.get("/path").andExpect { 4 status { isOk() } // (1) 5 request { asyncStarted() } // (2) 6 // TODO Remove unused generic parameter 7 request { asyncResult<Nothing>("body") } // (3) 8 }.andReturn() 9 10 mockmvc.perform(asyncDispatch(mvcResult)) // (4) 11 .andExpect { 12 status { isOk() } // (5) 13 content().string("body") 14 } 15}
| 1 | response status가 여전히 변경되지 않았는지 확인 |
| 2 | async processing이 시작되었어야 함 |
| 3 | async result를 대기하고 assert |
| 4 | (실행 중인 컨테이너가 없으므로) 수동으로 ASYNC dispatch 수행 |
| 5 | 최종 response 검증 |
Defining Expectations
Streaming Responses