التقاط الوسائط باستخدام ArgumentCaptor
استخدم ArgumentCaptor الخاص بـ Mockito لالتقاط الكائنات الدقيقة الممررة إلى بديل وهمي والتحقق منها في السيناريوهات المعقدة.
التقاط الوسائط باستخدام ArgumentCaptor درس مجاني في 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 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Inspecting What Was Passed
Sometimes verifying that a method was called is not enough. You need to assert on the actual argument the code constructed and passed. Mockito's ArgumentCaptor grabs that value for inspection.
Matchers vs Captors
Argument matchers like eq(...) check inputs during verification. A captor instead records the argument so you can run rich assertions afterward.
Creating a Captor
Declare a captor for the type you expect to capture.
ArgumentCaptor<User> captor =
ArgumentCaptor.forClass(User.class);Capturing During Verify
Pass the captor's capture() where the argument would go inside verify.
verify(repo).save(captor.capture());Reading the Value
After verification, retrieve the captured object and assert on its fields.
User saved = captor.getValue();
assertEquals("Ada", saved.getName());Full Example
Here we confirm the service builds the correct user before saving it.
service.register("Ada", "ada@x.io");
verify(repo).save(captor.capture());
assertEquals("ada@x.io",
captor.getValue().getEmail());Capturing Multiple Calls
If the method was called several times, use getAllValues() to get every captured argument in order.
verify(repo, times(2)).save(captor.capture());
List<User> all = captor.getAllValues();The @Captor Annotation
You can declare captors as fields with @Captor when using the Mockito extension, reducing boilerplate.
@Captor
ArgumentCaptor<User> captor;Captor or Matcher?
Use a matcher for simple equality checks at call time. Reach for a captor when the argument is complex, built internally, or needs multiple assertions.
Avoid Overuse
Captors couple tests to internal construction details. Prefer them for genuinely complex arguments, not for everything, to keep tests resilient.
Why It Matters
Captors let you verify the precise data your code hands to collaborators, catching subtle mapping or transformation bugs.
Quick Check
How do you retrieve the captured argument after verification?
Recap
You learned argument capturing:
ArgumentCaptor.forClassor@Captorcreates a captor- Pass
capture()insideverify getValue()/getAllValues()read the captured data- Use captors for complex, internally built arguments
الأسئلة الشائعة
هل درس «التقاط الوسائط باستخدام ArgumentCaptor» مجاني؟
نعم — نص درس «التقاط الوسائط باستخدام ArgumentCaptor» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Testing Mastery: JUnit, Mockito & Integration Tests، انتقل إلى CoddyKit PRO. تتضمن دورة Testing Mastery: JUnit, Mockito & Integration Tests 4 دروس في المجموع.
ماذا ستتعلم في «التقاط الوسائط باستخدام ArgumentCaptor»؟
استخدم ArgumentCaptor الخاص بـ Mockito لالتقاط الكائنات الدقيقة الممررة إلى بديل وهمي والتحقق منها في السيناريوهات المعقدة. تتمرن على Testing Mastery: JUnit, Mockito & Integration Tests مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Testing Mastery: JUnit, Mockito & Integration Tests؟
لا تُشترط خبرة سابقة. Testing Mastery: JUnit, Mockito & Integration Tests على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «التقاط الوسائط باستخدام ArgumentCaptor»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Testing Mastery: JUnit, Mockito & Integration Tests هذا؟
نعم. كل درس في Testing Mastery: JUnit, Mockito & Integration Tests يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الإجابات المخصّصة وعمليات الاستدعاء
- محاكاة الطرق الساكنة والمنشئات
- أفضل ممارسات Mockito
- التقاط الوسائط باستخدام ArgumentCaptor