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

المحاكاة (Mocking)

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

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

نظرة عامة

الموضوعات

  • Mockito
  • Stubs
  • Mocks
  • Spies
  • التحقق من التفاعلات

تعلّم أولًا

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

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

  1. Distinguish stubs, mocks, spies, and fakes, and pick one.
  2. Stub return values and exceptions, and verify interactions with Mockito.
  3. Capture arguments and assert on them.
  4. Recognize over-mocking and prefer fakes or real objects where cheap.

الدروس

  1. 01تصنيف البدائل الاختبارية (Test doubles)
  2. 02الـ Stubbing مع Mockito
  3. 03التحقق من التفاعلات
  4. 04ملتقطات الوسائط (Argument captors)
  5. 05متى لا نستخدم المحاكاة
  6. 06عرض عملي: محاكاة النتائج الثلاث لبوابة الدفع

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

Lab

JNR-044-LAB1 · Order service tests

Outcomes: 1–4 · Time: ~2.5 h Test OrderService with a mocked payment gateway (success, decline, timeout) and an in-memory fake repository. Acceptance: tests survive an internal refactor of the service.

Exercises (assessment: refactor an over-mocked test suite)

IDOutcomeTask
JNR-044-EX011Classify these as stub, mock, spy, or fake: canned exchange rate, in-memory ProductRepository, email sender checked for a call, real calculator with call counting.
JNR-044-EX022Stub PaymentGateway.charge to approve one amount and throw GatewayTimeoutException for another; test both.
JNR-044-EX032Verify that EmailSender.sendConfirmation is called once on success and never() on decline.
JNR-044-EX043Use an ArgumentCaptor to assert the saved Order has status PAID and the correct total.
JNR-044-EX054Given a test with eight mocks and 12 verify lines, remove the unnecessary verifies and replace the repository mock with a fake.
JNR-044-EX064A test mocks HttpClient directly. Redesign it with your own RateProvider interface.
JNR-044-EX074Explain what "mocks returning mocks" says about the design and how to fix it.
أظهر الحلول
  • EX01 Stub, fake, mock, spy.
  • EX02 when(gateway.charge(any(), eq(a))).thenReturn(approved) and when(gateway.charge(any(), eq(b))).thenThrow(new GatewayTimeoutException()); assert the result state of each order.
  • EX03 verify(emailSender).sendConfirmation(id) and verify(emailSender, never()).sendConfirmation(any()).
  • EX04 verify(repo).save(captor.capture()); assertThat(captor.getValue().status()).isEqualTo(PAID); plus a total assertion.
  • EX05 Keep only side-effect verifies (charge, email); assert on returned order and fake repository content.
  • EX06 Define interface RateProvider { BigDecimal rate(String code); } implemented by an HttpClient adapter; mock or fake RateProvider in unit tests; test the adapter separately.
  • EX07 The code reaches through several objects (law of Demeter) or the test is too coupled; pass the needed value in, or return a simple value from the first collaborator.
  • Outcome 1 is also covered by quiz Q01–Q02; the "survives a refactor" acceptance in the lab covers outcome 4.

الاختبار

Outcomes: O1 test doubles · O2 stubbing and verifying · O3 captors · O4 over-mocking.

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

  1. 1A working in-memory implementation of a repository is a:O1
  2. 2A stub mainly:O1
  3. 3Which stubs a return value?O2
  4. 4To simulate a timeout you use:O2
  5. 5verify(mock, never()).refund(any()) checks that:O2
  6. 6ArgumentCaptor is used to:O3
  7. 7After verify(repo).save(captor.capture()) you read it with:O3
  8. 8Which type should you NOT mock directly?O4
  9. 9Verifying every internal call makes tests:O4
  10. 10A mock returning another mock usually signals:O4

الجزء العملي

Seven exercises JNR-044-EX01 to EX07 plus the lab; for the assessment, refactor a provided over-mocked suite so it still passes after an internal refactor. Pass: quiz 8/10, at least five exercises correct, and the refactor accepted.

أخطاء شائعة

Mocking types you do not own; verifying every call; mocks returning mocks.

التقييم

Refactor an over-mocked test suite.

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

Contract testing (MID-045).

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