Döviz: Hata senaryosu
EuropeanCentralBankServer kütüphanesinin belgeleri, bankanın sunmadığı para birimlerine dönüştürme denemelerinin şu istisnayla sonuçlanacağını söylüyor:
new RuntimeException("Currency not in ECB list: " + currencyName)
Bu hata senaryosunu doğrulayan bir entegrasyon testi yaz.
Bu egzersiz
Java'da Teste Giriş
kursunun bir parçasıdırEgzersiz talimatları
- İstisnanın
RuntimeExceptionörneği olduğunu doğrulamak için doğru doğrulamayı (assertion) doldur. - Eşitliği kontrol etmek için doğru doğrulamayı kullan.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
class Main {
public static void main(String[] args) {
launchTestsAndPrint(ExchangeAppTest.class);
}
}
class ExchangeAppTest {
@Test
void convert_throwsException_whenGetRateThrowsException() {
EuropeanCentralBankServer bank = new EuropeanCentralBankServer();
ExchangeApp exchangeApp = new ExchangeApp(bank);
Exception expectedException = null;
try {
double result = exchangeApp.convertEuroTo("Invalid Currency", 1000);
} catch (Exception e) {
expectedException = e;
}
// Assert that the exception is an instance of the correct class
____(RuntimeException.class, expectedException);
// Assert that the exception message is correct
____("Currency not in ECB list: Invalid Currency", expectedException.getMessage());
}
}