Valuta: Foutscenario
De documentatie van de EuropeanCentralBankServer-bibliotheek geeft aan dat proberen te converteren naar valuta die de bank
niet aanbiedt, zal resulteren in deze exceptie:
new RuntimeException("Currency not in ECB list: " + currencyName)
Schrijf een integratietest die dit foutscenario verifieert.
Deze oefening maakt deel uit van de cursus
Introductie tot testen in Java
Oefeninstructies
- Vul de juiste assertion in om te controleren dat de exceptie een instantie van
RuntimeExceptionis. - Gebruik de juiste assertion om gelijkheid te controleren.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
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());
}
}