Cambio valuta: scenario di errore
La documentazione della libreria EuropeanCentralBankServer indica che il tentativo di convertire in valute non supportate dalla banca
produrrà questa eccezione:
new RuntimeException("Currency not in ECB list: " + currencyName)
Scrivi un test di integrazione che verifichi questo scenario di errore.
Questo esercizio fa parte del corso
Introduzione al Testing in Java
Istruzioni dell'esercizio
- Inserisci l'asserzione corretta per verificare che l'eccezione sia un'istanza di
RuntimeException. - Usa l'asserzione corretta per controllare l'uguaglianza.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
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());
}
}