การโยนข้อยกเว้นและการเรียกต่อเนื่อง
ตั้งค่าม็อกให้โยนข้อยกเว้นและคืนค่าที่แตกต่างกันในการเรียกต่อเนื่อง เพื่อสร้างสถานการณ์การทดสอบที่หลากหลายยิ่งขึ้น
การโยนข้อยกเว้นและการเรียกต่อเนื่อง เป็นบทเรียน 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 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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/doThrowsimulate errors- Multiple
thenReturnargs or chains give consecutive values - Mocks can only throw declared checked exceptions
- Use
do*for void methods and spies
คำถามที่พบบ่อย
บทเรียน “การโยนข้อยกเว้นและการเรียกต่อเนื่อง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การโยนข้อยกเว้นและการเรียกต่อเนื่อง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Testing Mastery: JUnit, Mockito & Integration Tests ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การโยนข้อยกเว้นและการเรียกต่อเนื่อง”
ตั้งค่าม็อกให้โยนข้อยกเว้นและคืนค่าที่แตกต่างกันในการเรียกต่อเนื่อง เพื่อสร้างสถานการณ์การทดสอบที่หลากหลายยิ่งขึ้น คุณปฏิบัติ Testing Mastery: JUnit, Mockito & Integration Tests ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การกำหนดค่าที่ส่งกลับจากสตับ
- ตัวจับคู่อาร์กิวเมนต์ของ Mockito
- การสอดส่องออบเจ็กต์จริง
- การโยนข้อยกเว้นและการเรียกต่อเนื่อง