通貨換算
あなたは通貨取引プラットフォームに取り組んでいます。通貨間の換算を行うユーティリティクラスがあり、その公開メソッド convert() は、
最初の通貨での金額と為替レートを受け取り、別の通貨での値を返します。
あなたのタスクは、テストを書いてこの仕様をコードベースの一部にすることです。まずは、このメソッドの成功パターンに対する ユニットテストを書くところから始めましょう。
必要な JUnit パッケージはすでにインポートされています。
この演習はコースの一部です
Javaによるテスト入門
演習の手順
- 通貨の金額と為替レートを指定して
convert()メソッドを呼び出してください。 - 適切な JUnit のアサーションを使って、
convertedCurrencyの金額が期待値と等しいことを確認してください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
public class CurrencyConverterWithTests {
public static void main(String[] args) {
launchTestsAndPrint(CurrencyConverterTest.class);
}
}
class CurrencyConverter {
public static double convert(double currency, double exchangeRate) {
return currency * exchangeRate;
}
}
class CurrencyConverterTest {
@Test
void convert_returnsConvertedCurrency_whenValidInputs() {
double currency = 100;
double exchangeRate = 1.2;
// Call the convert method with the currency and exchange rate
double convertedCurrency = CurrencyConverter.____(currency, ____);
// Use the correct JUnit assertion, the expected value, and the converted currency amount
____(120, ____);
}
}