0Pricing
Spring Boot 4 Microservices & REST APIs · Ders

MockMvc ile Web Katmanı Testleri

Controller'ları @WebMvcTest ile test edin.

MockMvc ile Web Katmanı Testleri, CoddyKit'te ücretsiz bir Spring Boot 4 Microservices & REST APIs dersidir. Bu, 4 dersinin 2. 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 Microservices & REST APIs öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Spring Boot 4 Microservices & REST APIs kursu toplamda 4 dersten oluşur.

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

Testing the Web Layer

Web layer tests verify your controllers — request mapping, validation, serialization, and status codes — without starting a full server.

Spring Boot provides @WebMvcTest and MockMvc for this.

@WebMvcTest

@WebMvcTest loads only the web slice: controllers, filters, and converters. Services and repositories are not loaded — you mock them.

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

    @MockBean
    UserService userService;
}

MockMvc.perform

MockMvc simulates HTTP requests against your controller without a network.

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

Mocking the Service

Use @MockBean to replace the service in the context, then stub it with Mockito.

@Test
void returnsUser() throws Exception {
    when(userService.getById("1"))
        .thenReturn(new User("1", "Alice"));

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

Asserting JSON with jsonPath

jsonPath verifies fields in the JSON response body.

mockMvc.perform(get("/users/1"))
    .andExpect(status().isOk())
    .andExpect(jsonPath("$.name").value("Alice"))
    .andExpect(jsonPath("$.id").value("1"));

Testing POST Requests

Send a JSON body with content and contentType.

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

Asserting Error Statuses

Verify 404 or 400 responses for missing resources or invalid input.

when(userService.getById("99"))
    .thenThrow(new NotFoundException());

mockMvc.perform(get("/users/99"))
    .andExpect(status().isNotFound());

Testing Validation

When a request fails bean validation, expect a 400 Bad Request.

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

Checking Headers

Assert response headers such as Location after a create.

mockMvc.perform(post("/users")
    .contentType(MediaType.APPLICATION_JSON)
    .content("{\"name\":\"Bob\"}"))
    .andExpect(header().string("Location",
        "/users/2"));

Query Parameters

Add request parameters with param.

mockMvc.perform(get("/users")
    .param("name", "Alice"))
    .andExpect(status().isOk())
    .andExpect(jsonPath("$.length()").value(1));

Printing the Result

Use andDo(print()) to log the full request and response while debugging.

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

Quick Check

Test your understanding of MockMvc.

Recap

You learned to test controllers in isolation:

  • @WebMvcTest loads only the web slice
  • MockMvc.perform simulates requests
  • @MockBean replaces services
  • jsonPath, status(), header() for assertions

Sıkça Sorulan Sorular

“MockMvc ile Web Katmanı Testleri” dersi ücretsiz mi?

Evet — “MockMvc ile Web Katmanı Testleri” 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 Microservices & REST APIs kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Spring Boot 4 Microservices & REST APIs kursu toplamda 4 dersten oluşur.

“MockMvc ile Web Katmanı Testleri” dersinde ne öğreneceğim?

Controller'ları @WebMvcTest ile test edin. Spring Boot 4 Microservices & REST APIs 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 Microservices & REST APIs öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Spring Boot 4 Microservices & REST APIs, 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 2. dersidir.

“MockMvc ile Web Katmanı Testleri” 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 Microservices & REST APIs dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Spring Boot 4 Microservices & REST APIs 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. MockMvc ile Web Katmanı Testleri
  3. @SpringBootTest ile Entegrasyon Testleri
  4. Testcontainers ile Gerçek Bağımlılıklar
← Spring Boot 4 Microservices & REST APIs Sayfasına Dön