@WebMvcTest ve MockMvc ile Web Katmanlarını Test Etme
Tam uygulama bağlamını başlatmadan Spring Boot denetleyicilerini @WebMvcTest ve MockMvc kullanarak yalıtılmış biçimde katman testiyle sınayın.
@WebMvcTest ve MockMvc ile Web Katmanlarını Test Etme, CoddyKit'te ücretsiz bir Testing Mastery: JUnit, Mockito & Integration Tests 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, Testing Mastery: JUnit, Mockito & Integration Tests öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Testing Mastery: JUnit, Mockito & Integration Tests kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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:
@WebMvcTestloads only controllers and web infrastructureMockMvcsimulates HTTP requests without a server@MockBeansupplies mocked services- Assert status and body with
jsonPath
Sıkça Sorulan Sorular
“@WebMvcTest ve MockMvc ile Web Katmanlarını Test Etme” dersi ücretsiz mi?
Evet — “@WebMvcTest ve MockMvc ile Web Katmanlarını 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 Testing Mastery: JUnit, Mockito & Integration Tests kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Testing Mastery: JUnit, Mockito & Integration Tests kursu toplamda 4 dersten oluşur.
“@WebMvcTest ve MockMvc ile Web Katmanlarını Test Etme” dersinde ne öğreneceğim?
Tam uygulama bağlamını başlatmadan Spring Boot denetleyicilerini @WebMvcTest ve MockMvc kullanarak yalıtılmış biçimde katman testiyle sınayın. Testing Mastery: JUnit, Mockito & Integration Tests 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.
Testing Mastery: JUnit, Mockito & Integration Tests öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Testing Mastery: JUnit, Mockito & Integration Tests, 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.
“@WebMvcTest ve MockMvc ile Web Katmanlarını 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 Testing Mastery: JUnit, Mockito & Integration Tests dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Testing Mastery: JUnit, Mockito & Integration Tests 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
- Spring Sınama Bağlamı Çerçevesi
- RESTful API'leri Sınama
- Sınamalar için Gömülü Veritabanları
- @WebMvcTest ve MockMvc ile Web Katmanlarını Test Etme