0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · درس

اختبار طبقات الويب باستخدام @WebMvcTest وMockMvc

اختبر وحدات تحكم Spring Boot بمعزل باستخدام @WebMvcTest وMockMvc من دون تشغيل سياق التطبيق الكامل.

اختبار طبقات الويب باستخدام @WebMvcTest وMockMvc درس مجاني في Testing Mastery: JUnit, Mockito & Integration Tests على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Testing Mastery: JUnit, Mockito & Integration Tests، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Testing Mastery: JUnit, Mockito & Integration Tests 4 دروس في المجموع.

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

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:

  • @WebMvcTest loads only controllers and web infrastructure
  • MockMvc simulates HTTP requests without a server
  • @MockBean supplies mocked services
  • Assert status and body with jsonPath

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

هل درس «اختبار طبقات الويب باستخدام @WebMvcTest وMockMvc» مجاني؟

نعم — نص درس «اختبار طبقات الويب باستخدام @WebMvcTest وMockMvc» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Testing Mastery: JUnit, Mockito & Integration Tests، انتقل إلى CoddyKit PRO. تتضمن دورة Testing Mastery: JUnit, Mockito & Integration Tests 4 دروس في المجموع.

ماذا ستتعلم في «اختبار طبقات الويب باستخدام @WebMvcTest وMockMvc»؟

اختبر وحدات تحكم Spring Boot بمعزل باستخدام @WebMvcTest وMockMvc من دون تشغيل سياق التطبيق الكامل. تتمرن على Testing Mastery: JUnit, Mockito & Integration Tests مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Testing Mastery: JUnit, Mockito & Integration Tests؟

لا تُشترط خبرة سابقة. Testing Mastery: JUnit, Mockito & Integration Tests على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «اختبار طبقات الويب باستخدام @WebMvcTest وMockMvc»؟

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

هل يمكنني كتابة وتشغيل أكواد في درس Testing Mastery: JUnit, Mockito & Integration Tests هذا؟

نعم. كل درس في Testing Mastery: JUnit, Mockito & Integration Tests يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

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

  1. إطار سياق الاختبار في Spring
  2. اختبار RESTful APIs
  3. قواعد البيانات المضمّنة للاختبارات
  4. اختبار طبقات الويب باستخدام @WebMvcTest وMockMvc
← العودة إلى Testing Mastery: JUnit, Mockito & Integration Tests