İstisna Fırlatma ve Ardışık Çağrılar
Daha zengin test senaryoları için sahte nesneleri istisna fırlatacak ve ardışık çağrılarda farklı değerler döndürecek şekilde yapılandırın.
İstisna Fırlatma ve Ardışık Çağrılar, CoddyKit'te ücretsiz bir Testing Mastery: JUnit, Mockito & Integration Tests dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Testing Mastery: JUnit, Mockito & Integration Tests öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Testing Mastery: JUnit, Mockito & Integration Tests kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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
Sıkça Sorulan Sorular
“İstisna Fırlatma ve Ardışık Çağrılar” dersi ücretsiz mi?
Evet — “İstisna Fırlatma ve Ardışık Çağrılar” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Testing Mastery: JUnit, Mockito & Integration Tests kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Testing Mastery: JUnit, Mockito & Integration Tests kursu toplamda 4 dersten oluşur.
“İstisna Fırlatma ve Ardışık Çağrılar” dersinde ne öğreneceğim?
Daha zengin test senaryoları için sahte nesneleri istisna fırlatacak ve ardışık çağrılarda farklı değerler döndürecek şekilde yapılandırın. Testing Mastery: JUnit, Mockito & Integration Tests ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Testing Mastery: JUnit, Mockito & Integration Tests öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Testing Mastery: JUnit, Mockito & Integration Tests, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.
“İstisna Fırlatma ve Ardışık Çağrılar” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Testing Mastery: JUnit, Mockito & Integration Tests dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Testing Mastery: JUnit, Mockito & Integration Tests dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Dönüş Değerlerini Taslaklama
- Mockito Bağımsız Değişken Eşleştiricileri
- Gerçek Nesneleri Gözetleme
- İstisna Fırlatma ve Ardışık Çağrılar