اختبارات طبقة الويب باستخدام MockMvc
اختبر وحدات التحكم باستخدام @WebMvcTest
اختبارات طبقة الويب باستخدام MockMvc درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Boot 4 Microservices & REST APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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:
@WebMvcTestloads only the web sliceMockMvc.performsimulates requests@MockBeanreplaces servicesjsonPath,status(),header()for assertions
الأسئلة الشائعة
هل درس «اختبارات طبقة الويب باستخدام MockMvc» مجاني؟
نعم — نص درس «اختبارات طبقة الويب باستخدام MockMvc» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.
ماذا ستتعلم في «اختبارات طبقة الويب باستخدام MockMvc»؟
اختبر وحدات التحكم باستخدام @WebMvcTest تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟
لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «اختبارات طبقة الويب باستخدام MockMvc»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟
نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- اختبار الوحدات باستخدام JUnit وMockito
- اختبارات طبقة الويب باستخدام MockMvc
- اختبارات التكامل باستخدام @SpringBootTest
- التبعيات الحقيقية باستخدام Testcontainers