始める無料で始める

為替: エラーシナリオ

EuropeanCentralBankServer ライブラリのドキュメントによると、銀行が提供していない通貨への変換を試みると、次の例外が発生します。

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

このエラーシナリオを検証する統合テストを書いてください。

この演習はコースの一部です

Javaによるテスト入門

コースを見る

演習の手順

  • 例外が RuntimeException のインスタンスであることを検証する正しいアサーションを記入してください。
  • 等価性を確認するための正しいアサーションを使用してください。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

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());
    }
}
コードを編集して実行