Unit test: Foreign exchange exceptions
One possible error scenario for the exchange app is the bank server being unavailable due to an outage or network issues. To ensure against disruptive exceptions, you have coded in a try/catch inside the convertEuroTo method.
Simulate the error scenario by making the mock EuropeanCentralBankServer object throw an exception when a rate is requested.
Diese Übung ist Teil des Kurses
Introduction to Testing in Java
Anleitung zur Übung
- Make the mock bank object throw an exception when
getRateEuroTois called. - Create the exact exception object you want to simulate.
- Verify the
convertEuroTomethod does not throw an exception but returns-1in this scenario.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
class Main {
public static void main(String[] args) {
launchMockitoTestsAndPrint(ExchangeAppTest.class);
}
}
class ExchangeAppTest {
@Test
void convertEuroTo_throwsException_whenBankUnavailable() {
EuropeanCentralBankServer bank = mock(EuropeanCentralBankServer.class);
ExchangeApp exchangeApp = new ExchangeApp(bank);
double euroAmount = 450;
// Stub the mock to throw an exception
when(exchangeApp.convertEuroTo("TST", 100)).____(new ____("Bank server is unavailable."));
double tstAmount = exchangeApp.convertEuroTo("TST", euroAmount);
// Assert on the return value of the method
____(____, ____);
}
}