Wymiana walut: scenariusz błędu
Dokumentacja biblioteki EuropeanCentralBankServer mówi, że próba konwersji na walutę, której bank nie obsługuje, spowoduje zgłoszenie następującego wyjątku:
new RuntimeException("Currency not in ECB list: " + currencyName)
Napisz test integracyjny, który weryfikuje ten scenariusz błędu.
To ćwiczenie jest częścią kursu
Wprowadzenie do testowania w Javie
Instrukcje do ćwiczenia
- Uzupełnij odpowiednie asercje, aby sprawdzić, czy wyjątek jest instancją klasy
RuntimeException. - Użyj właściwej asercji do sprawdzenia równości.
Interaktywne ćwiczenie praktyczne
Spróbuj tego ćwiczenia, uzupełniając ten przykładowy kod.
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());
}
}