ComeçarComece de graça

Foreign Exchange: Error scenario

The documentation for the EuropeanCentralBankServer library says that attempting to convert to currencies the bank doesn't offer will result in this exception:

new RuntimeException("Currency not in ECB list: " + currencyName)

Write an integration test that verifies this error scenario.

Este exercício faz parte do curso

Introduction to Testing in Java

Ver curso

Instruções do exercício

  • Fill in the correct assertion to verify the exception is an instance of RuntimeException.
  • Use the correct assertion to check equality.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

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());
    }
}
Editar e executar o código