0Pricing
Spring Boot 4 Complete Guide · Ders

Web Denetleyicilerini MockMvc ile Test Etme

Gerçek bir sunucuyu başlatmadan HTTP isteklerini benzetmek için MockMvc kullanarak REST uç noktalarınız için odaklı testler yazın.

Web Denetleyicilerini MockMvc ile Test Etme, CoddyKit'te ücretsiz bir Spring Boot 4 Complete Guide dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Spring Boot 4 Complete Guide öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Spring Boot 4 Complete Guide kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

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.

Sıkça Sorulan Sorular

“Web Denetleyicilerini MockMvc ile Test Etme” dersi ücretsiz mi?

Evet — “Web Denetleyicilerini MockMvc ile Test Etme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Spring Boot 4 Complete Guide kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Spring Boot 4 Complete Guide kursu toplamda 4 dersten oluşur.

“Web Denetleyicilerini MockMvc ile Test Etme” dersinde ne öğreneceğim?

Gerçek bir sunucuyu başlatmadan HTTP isteklerini benzetmek için MockMvc kullanarak REST uç noktalarınız için odaklı testler yazın. Spring Boot 4 Complete Guide ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Spring Boot 4 Complete Guide öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Spring Boot 4 Complete Guide, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.

“Web Denetleyicilerini MockMvc ile Test Etme” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Spring Boot 4 Complete Guide dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Spring Boot 4 Complete Guide dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. JUnit ve Mockito ile Birim Testleri
  2. Spring Boot ile Entegrasyon Testleri
  3. Katman Testleri ve TestContainers
  4. Web Denetleyicilerini MockMvc ile Test Etme
← Spring Boot 4 Complete Guide Sayfasına Dön