انتقل إلى المحتوى
أمين أبوزيد
JNR-046

اختبار الـ API

  • 5 درس
  • 2.5 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • MockMvc
  • REST Assured
  • سيناريوهات النجاح والفشل

تعلّم أولًا

2.5 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Test endpoints end-to-end with REST Assured against a running application.
  2. Choose between MockMvc slice tests and full HTTP tests.
  3. Cover success, validation, authentication, authorization, and not-found scenarios systematically.
  4. Assert on response bodies and headers, not just status codes.

الدروس

  1. 01MockMvc مقابل HTTP حقيقي
  2. 02REST Assured
  3. 03مصفوفة سيناريوهات لكل نقطة نهاية
  4. 04اختبار نقاط النهاية المؤمَّنة
  5. 05عرض عملي: رحلة الدفع (Checkout) باستخدام REST Assured

المختبر والتمارين

Lab

JNR-046-LAB1 · Checkout journey

Outcomes: 1–4 · Time: ~2.5 h Automate register → login → add to cart → checkout → view order, plus the failure scenario for each step, with REST Assured against the running Shop. Acceptance: matrix documented; all scenarios automated.

Exercises (assessment: scenario matrix for an unseen endpoint)

IDOutcomeTask
JNR-046-EX011Write a REST Assured test for GET /api/products/{id} checking status, content type, and two fields.
JNR-046-EX022For five checks (validation, mapping, full journey, security filter chain, DB constraint) choose MockMvc or full HTTP and justify.
JNR-046-EX033Write a scenario matrix for POST /api/orders with at least six rows.
JNR-046-EX043Automate the 401, 403, and 404 rows of that matrix.
JNR-046-EX054Assert the Location header and an error body (code, message) on a 400 response.
JNR-046-EX063Prove user A cannot read user B's order.
أظهر الحلول
  • EX01 given().port(port).when().get("/api/products/1").then().statusCode(200).contentType(JSON).body("id", equalTo(1)).body("name", notNullValue());
  • EX02 MockMvc: validation, mapping. Full HTTP: journey, security filter chain, DB constraint (needs the real stack).
  • EX03 Rows: valid 201; empty cart 400; no token 401; foreign cart 403; unknown product 404; out of stock 409.
  • EX04 Omit the header for 401; use a second user's token for 403; use a non-existent id for 404.
  • EX05 .header("Location", containsString("/api/orders/")); error: .body("code", equalTo("VALIDATION_ERROR")).body("message", not(emptyString())).
  • EX06 Create an order as A, request it with B's token, expect 403 (or 404 if the design hides existence).
  • Outcome 2 is also covered by quiz Q01–Q02.

الاختبار

Outcomes: O1 REST Assured · O2 MockMvc versus HTTP · O3 scenario coverage · O4 assertions.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1REST Assured style is:O1
  2. 2To use the random server port in a test:O1
  3. 3@WebMvcTest tests:O2
  4. 4Best for a full user journey with security and database:O2
  5. 5A missing token should give:O3
  6. 6A user without the needed role should get:O3
  7. 7A scenario matrix helps you:O3
  8. 8An unknown resource id should give:O3
  9. 9Better assertion on a response body:O4
  10. 10Why check headers like Location?O4

الجزء العملي

Six exercises JNR-046-EX01 to EX06 plus the lab; for the assessment, write a scenario matrix for an unseen endpoint. Pass: quiz 8/10, at least five exercises correct, and all matrix rows automated in the lab.

أخطاء شائعة

Only happy paths; brittle assertions on entire JSON strings.

التقييم

Scenario matrix for an unseen endpoint.

الصلة بالمسار الوظيفي

Contract and performance testing (MID-045, MID-046).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.