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

إطلاق الاستثناءات والاستدعاءات المتتالية

اضبط البدائل الوهمية لإطلاق الاستثناءات وإرجاع قيم مختلفة عبر الاستدعاءات المتتالية، لبناء سيناريوهات اختبار أكثر ثراءً.

الدرس 4 من 413 خطوة

إطلاق الاستثناءات والاستدعاءات المتتالية درس مجاني في 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 دروس في المجموع.

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

Beyond Simple Returns

Stubbing return values covers the happy path, but real code must also handle errors and changing state. Mockito lets you stub mocks to throw exceptions and to vary their answers per call.

thenThrow Basics

Use thenThrow to make a stubbed method raise an exception, so you can test your error handling.

when(repo.findById(99L))
    .thenThrow(new NotFoundException());

Testing the Failure Path

Combine the throwing stub with assertThrows to verify your code reacts correctly.

@Test
void propagatesError() {
  when(repo.findById(99L)).thenThrow(new NotFoundException());
  assertThrows(NotFoundException.class,
      () -> service.load(99L));
}

Throwing from void Methods

For void methods you cannot use when(...) directly. Use doThrow(...).when(mock).method() instead.

doThrow(new IllegalStateException())
    .when(logger).flush();

Consecutive Return Values

Pass multiple arguments to thenReturn to return different values on successive calls. The last value repeats once exhausted.

when(counter.next())
    .thenReturn(1, 2, 3);

Chaining Stub Calls

You can also chain calls. Each call describes the next invocation in order.

when(counter.next())
    .thenReturn(1)
    .thenReturn(2)
    .thenThrow(new IllegalStateException());

Mixing Returns and Throws

A common pattern: succeed a few times, then fail. This models flaky resources like a retrying network client.

when(client.fetch())
    .thenReturn("ok")
    .thenThrow(new IOException("down"));

Checked Exceptions Caveat

A mock can only throw a checked exception that the stubbed method declares. Throwing an undeclared checked exception causes a runtime error from Mockito.

doReturn for Consecutive Values

The do* family also supports chaining and is required when stubbing spies or void methods.

doReturn("a")
    .doReturn("b")
    .when(provider).value();

Why This Matters

Exception and consecutive stubbing let you exercise retry logic, fallback paths, and stateful sequences without building complex fake objects.

Putting It Together

You can now drive a mock through a full sequence of success and failure to validate resilient code.

when(api.call())
    .thenReturn("first")
    .thenThrow(new TimeoutException());

Quick Check

How do you make a stubbed method return 1, then 2 on its second call?

Recap

You learned advanced stubbing:

  • thenThrow / doThrow simulate errors
  • Multiple thenReturn args or chains give consecutive values
  • Mocks can only throw declared checked exceptions
  • Use do* for void methods and spies
البدء مجانًا

تعلم Testing Mastery: JUnit, Mockito & Integration Tests مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

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

هل درس «إطلاق الاستثناءات والاستدعاءات المتتالية» مجاني؟

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

ماذا ستتعلم في «إطلاق الاستثناءات والاستدعاءات المتتالية»؟

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

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

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

كم من الوقت يستغرق درس «إطلاق الاستثناءات والاستدعاءات المتتالية»؟

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

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

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

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

  1. تهيئة القيم المُعادة
  2. مطابقات المعاملات في Mockito
  3. مراقبة الكائنات الحقيقية
  4. إطلاق الاستثناءات والاستدعاءات المتتالية
← العودة إلى Testing Mastery: JUnit, Mockito & Integration Tests