การจับอาร์กิวเมนต์ด้วย ArgumentCaptor
ใช้ ArgumentCaptor ของ Mockito เพื่อจับและตรวจสอบอ็อบเจ็กต์ที่ถูกส่งไปยังม็อกอย่างแม่นยำในสถานการณ์ซับซ้อน
การจับอาร์กิวเมนต์ด้วย ArgumentCaptor เป็นบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คำตอบแบบกำหนดเองและโคลแบ็ก
- การจำลองเมธอดสแตติกและตัวสร้าง
- แนวทางปฏิบัติที่ดีของ Mockito
- การจับอาร์กิวเมนต์ด้วย ArgumentCaptor