0Pricing
Spring Boot 4 Complete Guide · درس

اختبار وحدات التحكم في الويب باستخدام MockMvc

اكتبوا اختبارات مركزة لنقاط نهاية REST الخاصة بكم باستخدام MockMvc لمحاكاة طلبات HTTP دون تشغيل خادم فعلي.

اختبار وحدات التحكم في الويب باستخدام MockMvc درس مجاني في Spring Boot 4 Complete Guide على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Boot 4 Complete Guide، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Boot 4 Complete Guide 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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.

الأسئلة الشائعة

هل درس «اختبار وحدات التحكم في الويب باستخدام MockMvc» مجاني؟

نعم — نص درس «اختبار وحدات التحكم في الويب باستخدام MockMvc» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Complete Guide، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Complete Guide 4 دروس في المجموع.

ماذا ستتعلم في «اختبار وحدات التحكم في الويب باستخدام MockMvc»؟

اكتبوا اختبارات مركزة لنقاط نهاية REST الخاصة بكم باستخدام MockMvc لمحاكاة طلبات HTTP دون تشغيل خادم فعلي. تتمرن على Spring Boot 4 Complete Guide مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Complete Guide؟

لا تُشترط خبرة سابقة. Spring Boot 4 Complete Guide على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «اختبار وحدات التحكم في الويب باستخدام MockMvc»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Complete Guide هذا؟

نعم. كل درس في Spring Boot 4 Complete Guide يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. اختبار الوحدات باستخدام JUnit وMockito
  2. اختبار التكامل باستخدام Spring Boot
  3. اختبار الشرائح وTestContainers
  4. اختبار وحدات التحكم في الويب باستخدام MockMvc
← العودة إلى Spring Boot 4 Complete Guide