0Pricing
Spring Boot 4 Complete Guide · 강의

MockMvc로 웹 컨트롤러 테스트

실제 서버를 시작하지 않고 HTTP 요청을 시뮬레이션하도록 MockMvc를 사용해 REST 엔드포인트를 위한 집중적인 테스트를 작성합니다.

MockMvc로 웹 컨트롤러 테스트은(는) CoddyKit의 무료 Spring Boot 4 Complete Guide 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Spring Boot 4 Complete Guide 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Spring Boot 4 Complete Guide 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Why Test Controllers in Isolation?

Controller tests verify routing, request mapping, status codes, and JSON serialization without the cost of a full server. MockMvc makes this fast and precise.

Introducing @WebMvcTest

The @WebMvcTest annotation loads only the web layer, your controllers and their supporting beans, keeping the test slice small and quick.

@WebMvcTest(UserController.class)
class UserControllerTest {
}

Injecting MockMvc

Inside a @WebMvcTest, Spring provides an auto-configured MockMvc instance you autowire to perform simulated requests.

@Autowired
MockMvc mockMvc;

Performing a GET Request

Use the fluent API to perform a request and chain expectations on the response.

mockMvc.perform(get("/users/1"))
    .andExpect(status().isOk());

Mocking the Service Layer

Controllers depend on services, so you replace them with mocks using @MockBean. This isolates the controller from real business logic.

@MockBean
UserService userService;

Stubbing Behavior

Combine Mockito stubbing with the request to control what the service returns.

when(userService.find(1L)).thenReturn(new User("Ada"));

Asserting JSON Content

Use jsonPath to verify specific fields in the response body.

mockMvc.perform(get("/users/1"))
    .andExpect(jsonPath("$.name").value("Ada"));

Testing POST with a Body

Send a JSON payload and assert the created status, verifying your endpoint accepts and processes input correctly.

mockMvc.perform(post("/users")
    .contentType(MediaType.APPLICATION_JSON)
    .content("{\"name\":\"Ada\"}"))
    .andExpect(status().isCreated());

Verifying Validation Errors

Send invalid input and assert a 400 Bad Request to confirm your validation rules actually fire.

mockMvc.perform(post("/users")
    .contentType(MediaType.APPLICATION_JSON)
    .content("{}"))
    .andExpect(status().isBadRequest());

Verifying Interactions

Use Mockito's verify to confirm the controller actually called the service the expected number of times.

verify(userService).find(1L);

When to Use Full Integration Tests

MockMvc slice tests are fast but mock the service. For end-to-end confidence across the real stack, complement them with full @SpringBootTest integration tests.

Quick Check

Test your understanding of MockMvc controller testing.

Recap

You used @WebMvcTest with MockMvc to test endpoints, mocked services with @MockBean, and asserted status codes and JSON. These slice tests are a fast first line of defense.

자주 묻는 질문

“MockMvc로 웹 컨트롤러 테스트” 강의는 무료인가요?

네 — “MockMvc로 웹 컨트롤러 테스트” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Spring Boot 4 Complete Guide 강의 전체를 잠금 해제할 수 있습니다. Spring Boot 4 Complete Guide 강의에는 총 4개의 강의가 포함되어 있습니다.

“MockMvc로 웹 컨트롤러 테스트”에서 뭘 배우나요?

실제 서버를 시작하지 않고 HTTP 요청을 시뮬레이션하도록 MockMvc를 사용해 REST 엔드포인트를 위한 집중적인 테스트를 작성합니다. 브라우저에서 직접 실행하는 실습 코드로 Spring Boot 4 Complete Guide을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Spring Boot 4 Complete Guide을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Spring Boot 4 Complete Guide은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.

“MockMvc로 웹 컨트롤러 테스트” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Spring Boot 4 Complete Guide 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Spring Boot 4 Complete Guide 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. JUnit 및 Mockito를 활용한 단위 테스트
  2. Spring Boot 통합 테스트
  3. 슬라이스 테스트 및 TestContainers
  4. MockMvc로 웹 컨트롤러 테스트
← Spring Boot 4 Complete Guide(으)로 돌아가기