0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · 강의

@WebMvcTest와 MockMvc로 웹 계층 테스트하기

전체 애플리케이션 컨텍스트를 시작하지 않고 @WebMvcTest와 MockMvc를 사용하여 Spring Boot 제어기를 격리된 상태로 부분 테스트하세요.

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

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

Why Slice Tests?

Loading the entire Spring context for every controller test is slow. Spring Boot offers test slices that load only the beans you need. @WebMvcTest loads just the web layer.

What @WebMvcTest Loads

@WebMvcTest configures controllers, filters, and JSON converters, but not services or repositories. Those collaborators are supplied as mocks.

Declaring the Test

Target a single controller so the slice stays small and fast.

@WebMvcTest(UserController.class)
class UserControllerTest {
  @Autowired MockMvc mockMvc;
}

Mocking the Service

The controller's service dependency is replaced with a Mockito mock using @MockBean, which places the mock in the context.

@MockBean
UserService userService;

Performing a Request

MockMvc simulates HTTP requests against your controller without a running server.

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

Stubbing Then Asserting

Stub the mocked service, fire the request, and assert on the response body and status.

when(userService.find(1L))
    .thenReturn(new User(1L, "Ada"));
mockMvc.perform(get("/users/1"))
    .andExpect(jsonPath("$.name").value("Ada"));

Checking Status Codes

Validate error mappings, such as a 404 when the service signals a missing entity.

when(userService.find(9L))
    .thenThrow(new NotFoundException());
mockMvc.perform(get("/users/9"))
    .andExpect(status().isNotFound());

Testing POST with a Body

Send JSON content and verify the controller deserializes and responds correctly.

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

JsonPath Assertions

jsonPath expressions let you assert on nested fields, array sizes, and values inside the JSON response.

.andExpect(jsonPath("$.items.length()").value(3))

Slice vs Full Context

Use @WebMvcTest for focused, fast web-layer tests. Reserve @SpringBootTest for true end-to-end flows where you want the whole context wired.

Speed Benefits

Because only the web layer loads, these tests start in a fraction of the time of a full-context test, encouraging more thorough controller coverage.

Quick Check

How are service dependencies handled in a @WebMvcTest?

Recap

You learned web-layer slice testing:

  • @WebMvcTest loads only controllers and web infrastructure
  • MockMvc simulates HTTP requests without a server
  • @MockBean supplies mocked services
  • Assert status and body with jsonPath

자주 묻는 질문

“@WebMvcTest와 MockMvc로 웹 계층 테스트하기” 강의는 무료인가요?

네 — “@WebMvcTest와 MockMvc로 웹 계층 테스트하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Testing Mastery: JUnit, Mockito & Integration Tests 강의 전체를 잠금 해제할 수 있습니다. Testing Mastery: JUnit, Mockito & Integration Tests 강의에는 총 4개의 강의가 포함되어 있습니다.

“@WebMvcTest와 MockMvc로 웹 계층 테스트하기”에서 뭘 배우나요?

전체 애플리케이션 컨텍스트를 시작하지 않고 @WebMvcTest와 MockMvc를 사용하여 Spring Boot 제어기를 격리된 상태로 부분 테스트하세요. 브라우저에서 직접 실행하는 실습 코드로 Testing Mastery: JUnit, Mockito & Integration Tests을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Testing Mastery: JUnit, Mockito & Integration Tests을(를) 시작하는 데 경험이 필요한가요?

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

“@WebMvcTest와 MockMvc로 웹 계층 테스트하기” 강의는 얼마나 걸리나요?

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

이 Testing Mastery: JUnit, Mockito & Integration Tests 강의에서 코드를 작성하고 실행할 수 있나요?

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

이 강의의 모든 강의

  1. Spring 테스트 컨텍스트 프레임워크
  2. REST 방식 API 테스트
  3. 테스트용 임베디드 데이터베이스
  4. @WebMvcTest와 MockMvc로 웹 계층 테스트하기
← Testing Mastery: JUnit, Mockito & Integration Tests(으)로 돌아가기